Generating Monthly Returns from a ton of daily data
On 4 June 2009 at 21:05, Joshua Ulrich wrote:
| Here's some code that does what Brian suggested (using Gabor's style): It's also easy to do it by hand using basic factor() usage (as I just explained today to one of our interns :) : ## first create some dates as a simple sequence, and some pseudo pnl R> randomdates <- seq(Sys.Date(), Sys.Date()+90, by=1) R> randompnl <- zoo( seq(1, length(randomdates)), order.by=randomdates ) ## the key then is to create a factor for the desired aggregation, here by month R> datefactor <- as.factor( format(index(randompnl), "%m") ) ## and all it takes is a call to tapply() [ or by() ] with the data, factor ## variable and aggregation function R> tapply( coredata(randompnl), datefactor, sum ) 06 07 08 09 378 1333 2294 181 I even made it more complicated than necessary by keeping it as zoo objects etc pp. Hth, Dirk
Three out of two people have difficulties with fractions.