假设我有一个包含'a','b','c'列的数据框。是否可以使用可变数量的条件过滤数据框?
而不是:
df[df$a == "chicken" | df$a == "cow" | df$a == "dog" | ...,]
是否有类似的东西:
df[df$a == c("chicken", "cow", "dog"),]
谢谢!
答案 0 :(得分:2)
您可以使用%in%
v1 <- c("chicken", "cow", "dog")
df[df$a %in% v1,]
set.seed(24)
df <- data.frame(a= sample(c('chicken', 'cow', 'dog', 'elephant',
'cat'), 20, replace=TRUE), b=rnorm(20))
答案 1 :(得分:1)
您可以使用match或grepl
使用匹配:
df <- data.frame(a=c(1,2,3), b=c("cow","dog","cat"))
p<-c("cow","dog")
df1<- df[match(p,df$b),]
使用grepl:
df1<- df[grepl(paste(p, collapse="|") , df$b),]
答案 2 :(得分: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
使用条件
的子集> 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