I have a data.frame
with character data in one of the columns.
I would like to filter multiple options in the data.frame
from the same column. Is there an easy way to do this that I'm missing?
Example:
data.frame
name = dat
days name
88 Lynn
11 Tom
2 Chris
5 Lisa
22 Kyla
1 Tom
222 Lynn
2 Lynn
I'd like to filter out Tom
and Lynn
for example.
When I do:
target <- c("Tom", "Lynn")
filt <- filter(dat, name == target)
I get this error:
longer object length is not a multiple of shorter object length
Best Answer
You need
%in%
instead of==
:Produces
To understand why, consider what happens here:
Basically, we're recycling the two length
target
vector four times to match the length ofdat$name
. In other words, we are doing:In this case we don't get an error because I suspect your data frame actually has a different number of rows that don't allow recycling, but the sample you provide does (8 rows). If the sample had had an odd number of rows I would have gotten the same error as you. But even when recycling works, this is clearly not what you want. Basically, the statement
dat$name == target
is equivalent to saying:It so happens that the last value in your sample data frame is even and equal to "Lynn", hence the one
TRUE
above.To contrast,
dat$name %in% target
says:Very different. Here is the result:
Note your problem has nothing to do with
dplyr
, just the mis-use of==
.