[英]Use dot dot dot (…) to indicate columns returned from a self-defined function for a data.table object
I want to use ...
to indicate the variables I want to return from a self-defined function for a data.table
object.我想用
...
表示我想从自定义 function 返回的变量,用于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[.data.table
(d, , .(N =.N, ...), cyl) 中的错误:未找到 object 'cyl'
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?任何人都可以分享他们的解决方案吗?
A possible solution is using mget
in your function wich returns a list and then combining xyz
with that with c
.一个可能的解决方案是在您的 function 中使用
mget
,它返回一个列表,然后将xyz
与c
结合起来。 The columns that you want to add need to be specified as a character vector to make this work:您要添加的列需要指定为字符向量才能使其工作:
getvar = function(...){
return(d[, c(xyz = mean(hp), mget(...)), cyl])
}
getvar(c("mpg", "cyl", "disp"))
which gives:这使:
> getvar(c("mpg", "cyl", "disp")) 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 6: 6 122.28571 17.8 6 167.6 7: 6 122.28571 19.7 6 145.0 8: 4 82.63636 22.8 4 108.0 9: 4 82.63636 24.4 4 146.7 10: 4 82.63636 22.8 4 140.8....
Or as an alternative a slight variation of @Rhonak's answer (thx to @zx8754):或者作为替代@Rhonak 答案的轻微变化(感谢@zx8754):
getvar = function(...){
mc <- match.call(expand.dots = FALSE)
x <- as.character(mc$...)
d[, c(xyz = mean(hp), mget(x)), cyl]
}
getvar(mpg, cyl, disp)
To get this to work without quoting the column names, you'd have to use some non-standard evaluation tactics:要在不引用列名的情况下使其工作,您必须使用一些非标准的评估策略:
getvar = function(...){
vars <- substitute(list(xyz = mean(hp), ...))
return(d[, eval(vars), cyl])
}
getvar(mpg, cyl, disp)
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
...etc...
Building up on answer by @Konrad Rudolph here , we can write the function在@Konrad Rudolph here的回答基础上,我们可以编写 function
getvar = function(...){
dots = match.call(expand.dots = FALSE)$...
cols = sapply(dots, deparse)
d[, c(xyz = mean(hp), mget(cols)), cyl]
#thanks to @Jaap for simplified version
}
getvar(mpg, cyl, disp)
# cyl xyz mpg cyl disp
# 1: 6 122.29 21.0 6 160.0
# 2: 6 122.29 21.0 6 160.0
# 3: 6 122.29 21.4 6 258.0
# 4: 6 122.29 18.1 6 225.0
# 5: 6 122.29 19.2 6 167.6
# 6: 6 122.29 17.8 6 167.6
#....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.