Skip to content

Instantly share code, notes, and snippets.

@MilesMcBain

MilesMcBain/fools_five.R

Last active Jul 17, 2019
Embed
What would you like to do?
Fool's five
f <- function(...) {
function(df) with(df, ...)
}
footate <- function(.data, ...) {
dots <- list(...)
for (column in names(dots)) {
.data[[column]] <- dots[[column]](.data)
}
return (.data)
}
foolter <-function(.data, ...) {
dots <- list(...)
indexes <-
Reduce(function(a, b) a & b(.data),
init = dots[[1]](.data),
x = dots)
return (.data[indexes, ])
}
foolter(mtcars, f(cyl >= 6 & am == 1))
library(microbenchmark)
microbenchmark(
footate(mtcars, foo = f(mpg * cyl)),
mutate(mtcars, foo = mpg * cyl))
microbenchmark(
filter(mtcars, cyl >= 6 & am == 1),
foolter(mtcars, f(cyl >= 6 & am == 1))
)
## mixing local + global state
footate(mtcars, foo = f(mpg * cyl * a_global))
foolter(mtcars, f(cyl >= 6 & am == 1 & a_global))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment