I want to use ...
to indicate the variables I want to return from a self-defined function for a data.table
object. Here is a minimal replicable example:
library(data.table)
d = data.table(mtcars)
getvar = function(...){
return(d[,.(xyz = mean(hp), ...), cyl])
}
getvar(mpg, cyl, disp)
Error in
[.data.table
(d, , .(N = .N, ...), cyl) : object 'cyl' not found
What I wish to get is:
d[,.(xyz = mean(hp), mpg, cyl, disp), cyl]
# cyl xyz mpg cyl disp
# 1: 6 122.28571 21.0 6 160.0
# 2: 6 122.28571 21.0 6 160.0
# 3: 6 122.28571 21.4 6 258.0
# 4: 6 122.28571 18.1 6 225.0
# 5: 6 122.28571 19.2 6 167.6
Anyone can share their solutions?