[R] How to do operations on zoo/xts objects with Monthly and Daily periodicities

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 29 14:37:27 CEST 2011


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



More information about the R-help mailing list