[R] AsOf join in R

Robert A'gata rhelpacc at gmail.com
Wed Oct 5 05:41:06 CEST 2011


Hi,

I tried to google for any solution for asof join operator in R. But I
couldn't find one. The asof join operator AsOf(A,B) merges 2 time
series by looking for latest available value of B prior to each time
point in A. For example,

A <- xts(c(10,15,20,25),
order.by=as.POSIXct(c("2011-09-01","2011-09-09","2011-09-10","2011-09-15"))

B <- xts(c(1.1,1.5,1.3,1.7),
order.by=as.POSIXct(c("2011-08-31","2011-09-09","2011-09-11","2011-09-12"))

AsOf(A,B) should return

                    A       B
2011-09-01    10     1.1
2011-09-09    15     1.1     #  (because latest value B prior to
2011-09-09 is 1.1)
2011-09-10    20     1.5
2011-09-15    25     1.7

How do I write the above AsOf function in R? The merge function does
not do what I want because it will align points that have the same
time stamp together while what I want is actually latest value prior
to timestamp in A. Any example would be greatly appreciated. Thank
you.

Cheers,

Robert



More information about the R-help mailing list