0

I have an assignment where I need to use the subset() function on the iris dataset in R. Specifically, I have to work with only two of the three Species variables, setosa, virginica, versicolor. Somehow, I cant get the subset function to pick more than one of these if I attempt to make a new dataframe where I only have setosa and virginica. How do I subset more than one of a categorical variable?

Thank you so much for any help.

  • 3
    Does this answer your question? [Getting rows given multiple values of a column in R](https://stackoverflow.com/questions/29266006/getting-rows-given-multiple-values-of-a-column-in-r) – davidf Nov 22 '21 at 14:05
  • Yes, definitely a duplicate – GuedesBF Nov 22 '21 at 15:08

2 Answers2

0

Try using the %in% keyword:

library(datasets)
data(iris)
subset(iris, iris$Species %in% c("setosa", "versicolor"))
davidf
  • 170
  • 1
  • 1
  • 9
0

You can write a subset where you specify which species from the variable name "Species" you want

dfSetosaVirginica <- subset(iris , Species %in% c("setosa","virginica"))
Yacine Hajji
  • 1,124
  • 1
  • 3
  • 20