michel
michel

Reputation: 284

R: pass a list of filtering conditions into a dataframe

I have a dataframe like:

   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  

and a long list of boolean filtering conditions like

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

Is there a way using base R or dplyr that I can do something like

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

so that I can continually add to the condition list, instead of manually pasting them and using multiple &'s in the index?

The output should be

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

Upvotes: 2

Views: 1879

Answers (2)

Sumedh
Sumedh

Reputation: 4965

Using 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

Data

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"))

Upvotes: 3

thelatemail
thelatemail

Reputation: 93813

Base R version:

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

Upvotes: 4

Related Questions