I'm trying to write a simple wrapper around data.table
for split-apply-combine use (like aggregate
, ddply
etc.) in order to gain from data.table
's speed but not use its data structures or syntax. I have tried the following.
fold = function(df, by, ...)
{
library(data.table)
dt = data.table(df)
dt = dt[, eval(substitute(list(...))), by]
df = as.data.frame(dt)
return(df)
}
a = data.frame(x=c(1,1,1,2,2,2), y=runif(6))
b = fold(a, "x", y_min=min(y), y_max=max(y))
print(a)
print(b)
This works fine when I put it in a script and source
it, but when I put that function in a package, install it and try to use it, I get
Error in eval(expr, envir, enclos) : object 'y' not found
Calls: fold -> [ -> [.data.table -> [.data.frame -> eval -> eval
What am I doing wrong, apart from trying this in general?