I recently worked out away to use filter_
in a dynamic situation with Shiny (see that answer here) using package lazyeval. Depending on what you are doing, it could be relevant, although your actual situation may be simpler.
You could do something similar by creating a vector of variables you want to condition with and vector of the same length with the conditions for each variable.
library(lazyeval)
variables = c("country", "sex")
conditions = c("USA", "F")
Then you could loop through the variables/conditions, using interp
to create a list of the conditions you want to filter on.
dots = lapply(1:length(variables),
function(crit) interp(~y == z,
.values = list(y = as.name(variables[crit]),
z = conditions[crit])))
dots
[[1]]
~country == "USA"
<environment: 0x02eed660>
[[2]]
~sex == "F"
<environment: 0x02c6b388>
Then just use the resulting list in the .dots
argument of filter
. I'm using @joshuagordon's df
here.
filter_(df, .dots = dots)
sex country
1 F USA
2 F USA
3 F USA
4 F USA