Skip to content

temporal join

2 messages · mckenzig, Jeff Ryan

#
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.
2 days later
#
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:
foo 
2009-01-01 "a1"
2009-01-02 "a2"
2009-01-03 "a3"
foo 
2009-01-04 "b1"
foo  foo.1
2009-01-01 "a1" NA   
2009-01-02 "a2" NA   
2009-01-03 "a3" NA   
2009-01-04 NA   "b1"
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: