R: pass a list of filtering conditions to the data frame

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 such as

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

Is there a way to use the R or dplyr base so that I can do something like

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

so that I can constantly add conditions to the list, rather than manually insert them and use several in the index?

The exit should be

 filter(dataframe, Symbol in 'ABM' & Growth > 1.2 & Yield within (2 3) & PE>3 &...) 
+1
source share
2 answers

Basic version of 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 
+4
source

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")) 
+2
source

Source: https://habr.com/ru/post/1269887/


All Articles