dt <- data.table(x=1:4, y=c(1,1,2,2), z=c(1,2,1,2))
I would likt to achieve this:
dt[,list(z, p=cumsum(x)), by=y]
y z p
1: 1 1 1
2: 1 2 3
3: 2 1 3
4: 2 2 7
But via a function call like test(dt, z, x, y)
None of the following 2 ways works. data.table 1.8.10
test1 <- function(dt, a, b, c){
dt[,list(eval(substitute(a), parent.frame()),
p=cumsum(eval(substitute(b), parent.frame()))),
by=eval(substitute(c)), verbose=TRUE]
}
test1(dt, z, x, y)
# Error in eval(expr, envir, enclos) : object 'a' not found
test2 <- function(dt, a, b, c){
dt[,list(eval(substitute(a)),
p=cumsum(eval(substitute(b)))),
by=eval(substitute(c)), verbose=TRUE]
}
test2(dt, z, x, y)
# Error in eval(expr, envir, enclos) : object 'z' not found
What is a correct way to make it work?