传递参数到data.table聚合函数 [英] Pass argument to data.table aggregation function

查看:103
本文介绍了传递参数到data.table聚合函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数计算变量的加权平均值,并使用 data.table 聚合语法按时间段对其进行分组。但是,我想以编程方式提供加权列的名称。有没有办法来完成这个,而仍然使用传统的 data.table 语法?下面的函数 wtmean1 演示了我想做什么的想法(但它产生一个错误)。函数 wtmean2 起作用,并受到 data.table 常见问题的启发,但是传递整个表达式更麻烦,并且不可能提取函数中的加权列的名称,这可能是需要的。有没有办法让 wtmean1 工作,我传递的唯一的参数是字符串中的加权列的名称?

I have a function that calculates a weighted mean of a variable and groups it by time period using the data.table aggregation syntax. However, I want to provide the name of the weighting column programmatically. Is there a way to accomplish this while still using the traditional data.table syntax? The function wtmean1 below demonstrates the idea of what I want to do (but it produces an error). The function wtmean2 works and is inspired by the data.table FAQ, but it's more cumbersome to pass in the whole expression, and it's not possible to extract out the name of the weighting column within the function, which might be needed. Is there a way to get wtmean1 to work, where the only argument that I pass in is the name of the weighting column in a string?

wtmean1 <- function(dt1, weight) {
  dt1[,weighted.mean(x, weight), by=timeperiod]
}

wtmean2 <- function(dt1, expr) {
  dt1[,eval(substitute(expr)), by=timeperiod]
}

mydata <- data.table(x=1:10, timeperiod=rep(1:2,5), wt1=rnorm(10), wt2=rnorm(10))
wtmean1(mydata, "wt1") # ERROR
wtmean2(mydata, weighted.mean(x, wt2))


推荐答案

您可以使用 get

wtmean1 <- function(dt1, weight) {
  dt1[,weighted.mean(x, get(weight)), by=timeperiod]
}

使用您的示例数据:

> set.seed(1)
> mydata <- data.table(x=1:10, timeperiod=rep(1:2,5), wt1=rnorm(10), wt2=rnorm(10))
> wtmean1(mydata, "wt1")
   timeperiod          V1
1:          1 -102.476925
2:          2    3.362326

这篇关于传递参数到data.table聚合函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆