Inspired by the answer from @sven-hohenstein here is a generalised function that will filter based on a list of predicates, specified in the form column=list(binary_operator, rhs)
(e.g. x=list(`<=`, 3)
for x <= 3
).
#' Filter a data frame dynamically
#'
#' @param df data frame to filter
#' @param controls list of filters (with optional operators)
filter_data = function(df, controls) {
evaluate = function(predicate, value) {
if (is.list(predicate)) {
operator = predicate[[1L]]
rhs = predicate[[2L]]
} else {
operator = `==`
rhs = predicate
}
return(operator(value, rhs))
}
index = apply(
mapply(evaluate, predicate=controls, value=df[names(controls)]), 1L, all
)
return(df[index, ])
}
Here is an example using the filtering function to apply the condition x == 2 & y <= 2.5 & z != 'C'
:
# create example data
df = data.frame(
x=sample(1:3, 100L, TRUE),
y=runif(100L, 1, 5),
z=sample(c('A', 'B', 'C'), 100L, TRUE)
)
controls = list(x=2L, y=list(`<=`, 2.5), z=list(`!=`, 'C'))
filter_data(df, controls)