-2

Say that I have a dataframe with columns 'a', 'b', 'c'. Is it possible to filter the dataframe with a variable number of criteria?

Instead of:

df[df$a == "chicken" | df$a == "cow" | df$a == "dog" | ...,]

Is there something like:

df[df$a == c("chicken", "cow", "dog"),]

Thanks!

joran
  • 169,992
  • 32
  • 429
  • 468
user2813915
  • 59
  • 1
  • 5

3 Answers3

2

You can use %in%

 v1 <- c("chicken", "cow", "dog")
 df[df$a %in% v1,]

data

 set.seed(24)
 df <- data.frame(a= sample(c('chicken', 'cow', 'dog', 'elephant',
  'cat'), 20, replace=TRUE), b=rnorm(20))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You can use match or grepl

Using Match:

df <- data.frame(a=c(1,2,3), b=c("cow","dog","cat")) p<-c("cow","dog") df1<- df[match(p,df$b),]

Using grepl: df1<- df[grepl(paste(p, collapse="|") , df$b),]

rlpatrao
  • 565
  • 1
  • 8
  • 15
  • I think that match() only gets the first time those elements appear, and grepl() would also match elements where it is nested? i.e. "cow" in "cowel" This is a helpful start though! – user2813915 Apr 24 '15 at 04:36
0
> df <- expand.grid(A1=(1:3)*10,A2=1:3,A3=c("Chicken","dog"))
> df
   A1 A2      A3
1  10  1 Chicken
2  20  1 Chicken
3  30  1 Chicken
4  10  2 Chicken
5  20  2 Chicken
6  30  2 Chicken
7  10  3 Chicken
8  20  3 Chicken
9  30  3 Chicken
10 10  1     dog
11 20  1     dog
12 30  1     dog
13 10  2     dog
14 20  2     dog
15 30  2     dog
16 10  3     dog
17 20  3     dog
18 30  3     dog

Use Subset with the condition

> x <- with(df, (A2 == 2 | A3 == "dog"))
> subset(df, x)
   A1 A2      A3
4  10  2 Chicken
5  20  2 Chicken
6  30  2 Chicken
10 10  1     dog
11 20  1     dog
12 30  1     dog
13 10  2     dog
14 20  2     dog
15 30  2     dog
16 10  3     dog
17 20  3     dog
18 30  3     dog
Prasanna Nandakumar
  • 4,295
  • 34
  • 63