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
This is also easily done with aggregate. It helps to remember alternate equations for a weighted mean.
rw <- dat$return * dat$assets dat1 <- aggregate(rw ~ assetclass, data = dat, sum) datw <- aggregate(assets ~ assetclass, data = dat, sum) dat1$weighted.return <- dat1$rw / datw$assets