[R] [R-SIG-Finance] AsOf join in R

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 6 15:30:43 CEST 2011


On Thu, Oct 6, 2011 at 2:15 AM, Ulrich Staudinger <ustaudinger at gmail.com> wrote:
> A bit late,  but here is what I always do:
>
> m = merge(bid, ask, tick)
> m<- interpNA(m, method="before")
>
> intrepNA can also interpolate NAs in different ways, for example linearly.
>

Its not clear precisely what sort of objects bid, ask and tick were
intended to be in your example but none of the tries below produce 1.1
for Sep 9th as asked for:

> library(xts)
> library(timeSeries)
> 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")))
>
> #1
> interpNA(merge(A, B), method = "before")
            A   B
2011-08-31 NA 1.1
2011-09-01 10 1.1
2011-09-09 15 1.5
2011-09-10 20 1.5
2011-09-11 20 1.3
2011-09-12 20 1.7
2011-09-15 25  NA
>
> #2
> interpNA(merge(as.timeSeries(A), as.timeSeries(B)), method = "before")
GMT
           TS.1
2011-08-31  1.1
2011-09-01 10.0
2011-09-09  1.5
2011-09-09 15.0
2011-09-10 20.0
2011-09-11  1.3
2011-09-12  1.7
2011-09-15 25.0
>
> #3
> interpNA(as.timeSeries(merge(A, B)), method = "before")
GMT
            A   B
2011-08-31 NA 1.1
2011-09-01 10 1.1
2011-09-09 15 1.5
2011-09-10 20 1.5
2011-09-11 20 1.3
2011-09-12 20 1.7
2011-09-15 25  NA

interpNA seems to use approx underneath so its functionality is
similar to na.approx in zoo/xts but with different defaults.

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