I'm using the data iris in R.
I'm trying to create a data frame consisting of only the “Sepal.Length”, “Sepal.Width” and “Species” columns for only the rows of species “setosa” and “versicolor”, and assigning the data frame to name “df”. Species contains 3 different species and I only want 2 types in my new data frame. Ive tried so many things and Im still lost. Can someone help?
library(datasets)
library(dplyr)
data(iris)
summary(iris)
names(iris)
df = iris[,c("Sepal.Length","Sepal.Width")]
dim(df)
df = subset(Species, Species== "versicolor")
dim(df)
x = iris$Species == "versicolor"
df <- df[which(iris$Species =="versicolor"& "setosa" ),]
subset(df, Species %in% c("versicolor", "setosa"))
CodePudding user response:
Is this what you want?
library(dplyr)
df <- iris %>%
filter(Species != "virginica") %>%
select(starts_with("S"))
CodePudding user response:
Here is an option using subset
from base R:
subset(iris, Species != "vericolor", startsWith(names(iris), "S"))
Or without subset
:
iris[iris$Species != "vericolor", startsWith(names(iris), "S")]