KennyPeanuts KennyPeanuts - 2 months ago 14
R Question

Subset a data frame using OR when the column contains a factor

I would like to make a subset of a data frame in R that is based on one OR another value in a column of factors but it seems I cannot use

with factor values.


# fake data
x <- sample(1:100, 9)
nm <- c("a", "a", "a", "b", "b", "b", "c", "c", "c")
fake <- cbind(,
# subset fake to only rows with name equal to a or b
fake.trunk <- fake[fake$nm == "a" | "b", ]

produces the error:

Error in fake$nm == "a" | "b" :
operations are possible only for numeric, logical or complex types

How can I accomplish this?

Obviously my actual data frame has more than 3 values in the factor column so just using
!= "c"
won't work.


You need fake.trunk <- fake[fake$nm == "a" | fake$nm == "b", ]. A more concise way of writing that (especially with more than two conditions) is:

fake[ fake$nm %in% c("a","b"), ]