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

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

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

______________________________________________
R-help_at_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.
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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive