This question follows another one on group weighted means: I would like to create weighted within-group averages using data.table
. The difference with the initial question is that the names of the variables to be average are specified in a string vector.
The data:
df <- read.table(text= "
region state county weights y1980 y1990 y2000
1 1 1 10 100 200 50
1 1 2 5 50 100 200
1 1 3 120 1000 500 250
1 1 4 2 25 100 400
1 1 4 15 125 150 200
2 2 1 1 10 50 150
2 2 2 10 10 10 200
2 2 2 40 40 100 30
2 2 3 20 100 100 10
", header=TRUE, na.strings=NA)
Using Roland's suggested answer from aforementioned question:
library(data.table)
dt <- as.data.table(df)
dt2 <- dt[,lapply(.SD,weighted.mean,w=weights),by=list(region,state,county)]
I have a vector with strings to determine dynamically columns for which I want the within-group weighted average.
colsToKeep = c("y1980","y1990")
But I do not know how to pass it as an argument for the data.table magic.
I tried
dt[,lapply(
as.list(colsToKeep),weighted.mean,w=weights),
by=list(region,state,county)]`
but I then get:
Error in x * w : non-numeric argument to binary operator
Not sure how to achieve what I want.
Bonus question: I'd like original columns names to be kept, instead of getting V1 and V2.
NB I use version 1.9.3 of the data.table package.