luismiguelgpc
luismiguelgpc

Reputation: 11

Filter various text at the same time in r

I have a datafrme like this:

v1 v2 v3
2  4  hello
3  1  hello
4  5  hi
7  8  hi
6  0  greetings
8  0  greetings
9  7  hola

I need to filter from v3, hello, greetings and hola. I tried this:

x = filter(data$v3, c("hello","greetings","hola"))

but does not work, my original data is a very extensive dataframe with 110000 rows and 86 colums. I need to filter more than three. The dataframe I looking for is like:

v1 v2 v3
2  4  hello
3  1  hello
6  0  greetings
8  0  greetings
9  7  hola

Thank´s for your help!

Upvotes: 1

Views: 28

Answers (1)

Florian
Florian

Reputation: 25395

filter requires as second argument;

Logical predicates defined in terms of the variables in .data. Multiple conditions are combined with &. Only rows where the condition evaluates to TRUE are kept.

So we have to pass statements that return TRUE or FALSE for each row. You could use the %in% operator, for example as follows:

library(dplyr)
df %>% filter(v3 %in% c("hello","greetings","hola"))

which returns

  v1 v2        v3
1  2  4     hello
2  3  1     hello
3  6  0 greetings
4  8  0 greetings
5  9  7      hola

Hope this helps.

Upvotes: 1

Related Questions