The question is basically the samt as this: Aggregate and Weighted Mean in R.
But i want it to compute it on several columns, using data.table, as I have millions of rows. So something like this:
set.seed(42) # fix seed so that you get the same results
dat <- data.frame(assetclass=sample(LETTERS[1:5], 20, replace=TRUE),
tax=rnorm(20),tax2=rnorm(20), assets=1e7+1e7*runif(20), assets2=1e6+1e7*runif(20))
DT <- data.table(dat)
I can compute the weighted mean on one column, assets, like this:
DT[,list(wret = weighted.mean(tax,assets)),by=assetclass]
But how to do it on both assets and assets2?
What if there are several columns, like col=c("assets1", "assets2", "assets3", ... )
?
And is it also possible to do it for tax, tax1...