Pass argument to data.table aggregation function

早过忘川 提交于 2020-01-22 21:22:49

问题


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))

回答1:


You can use get:

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

With your sample data:

> 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


来源:https://stackoverflow.com/questions/20196994/pass-argument-to-data-table-aggregation-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!