R:将过滤条件列表传递到数据帧中

时间:2016-07-21 00:39:56

标签: r dplyr

我有一个类似的数据框:

   Symbol Yield    PE    Growth  
1    ABBV  3.46 18.80      5.00  
2     ABM  2.24 21.18      3.33  
3     ABT  2.26 23.65     10.85  
4     ADM  1.91 22.29      9.08  
5     ADP  2.46 25.83      8.57  
6     AFL  2.25  9.26      5.97  
7     ALB  1.44 13.53     13.15  
8    ANDE  1.02 19.59      5.74  
9     AOS  1.29 25.11      9.99  
10    APD  2.41 25.08      2.53  
11   ARLP  5.50 11.69      1.99  
12   AROW  3.83 14.68      1.01  
13  ARTNA  3.67 23.91      3.20  
14   ATNI  1.68  3.14      7.50  
15    ATO  2.97 18.59      1.72  

和一长串布尔过滤条件,如

conditions = c('Symbol in `ABM', 'Growth > 1.2', 'Yield within (2 3)', 'PE>3',....)

有没有办法使用base R或dplyr我可以做类似

的事情
for (condition in conditions) {    
cond = expression(condition)
    dataframe = dataframe[which(cond),]}

这样我就可以不断添加条件列表,而不是手动粘贴它们并在索引中使用多个& s?

输出应为

filter(dataframe, Symbol in 'ABM' & Growth > 1.2 & Yield within (2 3) & PE>3 &...)

2 个答案:

答案 0 :(得分:4)

Base R版本:

conditions <- with(dat, list(Symbol %in% "ABM", Growth > 1.2, Yield > 2, Yield < 3, PE > 3))
dat[Reduce(`&`, conditions ),]
#  Symbol Yield    PE Growth
#2    ABM  2.24 21.18   3.33

答案 1 :(得分:3)

使用dplyr

library(dplyr)

conditions = c('Symbol %in% "ABM"', 'Growth > 1.2', 'Yield > 2', 'Yield < 3', 'PE > 3')

df %>% filter_(conditions)


  Symbol Yield    PE Growth
1    ABM  2.24 21.18   3.33

数据

structure(list(Symbol = structure(1:15, .Label = c("ABBV", "ABM", 
"ABT", "ADM", "ADP", "AFL", "ALB", "ANDE", "AOS", "APD", "ARLP", 
"AROW", "ARTNA", "ATNI", "ATO"), class = "factor"), Yield = c(3.46, 
2.24, 2.26, 1.91, 2.46, 2.25, 1.44, 1.02, 1.29, 2.41, 5.5, 3.83, 
3.67, 1.68, 2.97), PE = c(18.8, 21.18, 23.65, 22.29, 25.83, 9.26, 
13.53, 19.59, 25.11, 25.08, 11.69, 14.68, 23.91, 3.14, 18.59), 
    Growth = c(5, 3.33, 10.85, 9.08, 8.57, 5.97, 13.15, 5.74, 
    9.99, 2.53, 1.99, 1.01, 3.2, 7.5, 1.72)), .Names = c("Symbol", 
"Yield", "PE", "Growth"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15"))