Skip to content
Back to formatted view

Raw Message

Message-ID: <18984.33261.753796.923449@ron.nulle.part>
Date: 2009-06-05T02:24:45Z
From: Dirk Eddelbuettel
Subject: Generating Monthly Returns from a ton of daily	data
In-Reply-To: <8cca69990906041905w711f77caq3b0fd247402a910c@mail.gmail.com>

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.