[R] temporal join
Jeff Ryan
jeff.a.ryan at gmail.com
Tue Jan 13 16:23:11 CET 2009
The data.table package may be more in line with what you are after, but xts
and zoo can also do what you need in this particular example:
> a <- xts(c('a1','a2','a3'), timeBasedSeq(20090101/20090103))
> colnames(a) <- 'foo'
> b <- xts(c('b1'), as.Date('2009-01-04'))
> colnames(b) <- 'foo'
> a
foo
2009-01-01 "a1"
2009-01-02 "a2"
2009-01-03 "a3"
> b
foo
2009-01-04 "b1"
> cbind(a,b)
foo foo.1
2009-01-01 "a1" NA
2009-01-02 "a2" NA
2009-01-03 "a3" NA
2009-01-04 NA "b1"
> na.locf(cbind(a,b))['20090104']
foo foo.1
2009-01-04 "a3" "b1"
cbind/merge will merge along the union on the time-index by default (though
all common joins are supported). The subsetting by time will then find the
dates (or range of dates/times) that match.
na.locf will carry forward last observations. That is from zoo; which works
on xts, as xts extends zoo.
HTH,
Jeff
mckenzig wrote:
>
> I have dataframe a:
>
> sym date val1
> === ==== ====
> foo 20090101 a1
> foo 20090102 a2
> foo 20090103 a3
>
> and dataframe b:
>
> sym date val2
> === ==== ====
> foo 20090104 b1
>
> I would like to join/merge them to generate the following:
>
> sym date val2 val1
> === ==== ==== ====
> foo 20090104 b1 a3
>
> i.e. an equijoin on column 'sym' and a temporal join on column 'date'
> where the closest matching row is retrieved. I have been through the
> various regular/irregular timeseries packages and can not see anything
> like this.
>
> Regards.
>
> ______________________________________________
> 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.
>
>
--
View this message in context: http://www.nabble.com/temporal-join-tp21395414p21437847.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list