medavis6 medavis6 - 1 year ago 69
R Question

Subset Data Frame by String AND Number, not Either Or

I'm wondering how to subset (or filter) a data frame by two columns (one is string, one is number) including only results where both conditions are met, not one or the other.

If I use the & operator, it returns data where either condition is met.


foo <- iris[which(iris$Petal.Length > 1.2 & iris$Species != "setosa"),]

This returns only versicolor and virginica results above 1.2, rather than the setosa results that are above 1.2 as well. How would I go about getting all results where the Petal Length is above 1.2 and if it is below 1.2 and is setosa it is removed?

I'm fine using
if that is the route you suggest going. Any other alternatives are welcome, as well.

Answer Source

We can try

library(data.table)[Species == "setosa" & Petal.Length> 1.2| 
                         Species != "setosa" & Petal.Length > 1.2]

Or with dplyr

iris %>%
     filter(Species == "setosa" & Petal.Length > 1.2 |
             Species != "setosa" & Petal.Length > 1.2)