I\'m trying to calculate asset-weighted returns by asset class. For the life of me, I can\'t figure out how to do it using the aggregate command.
My data frame look
A data.table solution, will be faster than plyr
data.table
plyr
library(data.table) DT <- data.table(dat) DT[,list(wret = weighted.mean(return,assets)),by=assetclass] ## assetclass wret ## 1: A -0.05445455 ## 2: E -0.56614312 ## 3: D -0.43007547 ## 4: B 0.69799701 ## 5: C 0.08850954