Skip to content

Instantly share code, notes, and snippets.

@MilesMcBain
Last active July 17, 2019 03:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MilesMcBain/c20029c239b1065255ec36441ffafa71 to your computer and use it in GitHub Desktop.
Save MilesMcBain/c20029c239b1065255ec36441ffafa71 to your computer and use it in GitHub Desktop.
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