thierrydb
2011-May-28 21:22 UTC
[R] How to do operations on zoo/xts objects with Monthly and Daily periodicities
Is there an elegant way to do operations (+/-/*/ / ) on zoo/xts objects when one serie is monthly (end of month) and the other daily (weekdays only) - typically a monthly economic indicator and a stock index price? Thanks, TDB -- View this message in context: http://r.789695.n4.nabble.com/How-to-do-operations-on-zoo-xts-objects-with-Monthly-and-Daily-periodicities-tp3558081p3558081.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2011-May-29 12:37 UTC
[R] How to do operations on zoo/xts objects with Monthly and Daily periodicities
On Sat, May 28, 2011 at 5:22 PM, thierrydb <thierrydb at gmail.com> wrote:> Is there an elegant way to do operations (+/-/*/ / ) on zoo/xts objects when > one serie is monthly (end of month) and the other daily (weekdays only) - > typically a monthly economic indicator and a stock index price? > > >Merge the monthly series with a zero width series having the same times as the daily series and then apply na.locf or na.approx to fill it in. Now the two daily series can be added, etc. library(zoo) ## create test data. z1 is daily and z2 is monthly tt <- as.Date(1:100) tt <- tt[! weekdays(tt) %in% c("Saturday", "Sunday")] z1 <- zoo(seq_along(tt), tt) ttm <- unique(as.yearmon(tt)) z2 <- zoo(seq_along(ttm), ttm) ## Now that we have some data, ## (1) convert z2 to days using locf z2locf <- na.locf(merge(aggregate(z2, as.Date), zoo(, time(z1)))) ## or (2) using interpolation z2approx <- na.approx(aggregate(z2, as.Date), xout = time(z1), rule = 2) z1 + z2locf z1 + z2approx -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com