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

From: Gabor Grothendieck <>
Date: Sun, 29 May 2011 08:37:27 -0400

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


## 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

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 29 May 2011 - 12:42:36 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 29 May 2011 - 15:00:10 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive