Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass argument to data.table aggregation function

Tags:

r

data.table

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))
like image 476
Abiel Avatar asked Nov 25 '13 15:11

Abiel


1 Answers

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
like image 118
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 15 '22 04:11

A5C1D2H2I1M1N2O1R2T1