On Sat, May 28, 2011 at 5:22 PM, thierrydb <thierr...@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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to