Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active December 12, 2015 01:28
Show Gist options
  • Save joshbode/4690896 to your computer and use it in GitHub Desktop.
Save joshbode/4690896 to your computer and use it in GitHub Desktop.
Dynamically evaluate stored conditions
# evaluate a condition
cond = function(expr, d=parent.frame()) {
Reduce(`&`, lapply(expr, eval, d))
}
# example
l = list(
'Condition 1'=expression(x > 0),
'Condition 2'=expression(y < 0)
)
set.seed(1)
d = data.frame(
x=trunc(runif(100, -10, 10)),
y=trunc(runif(100, -10, 10))
)
b = d$x > 0 & d$y < 0
identical(b, with(d, cond(l)))
identical(b, cond(l, d))
identical(d[b, ], subset(d, cond(l)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment