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))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With