[R] merging and obtaining the nearest value

Francesco cariboupad at gmx.fr
Sun Aug 19 12:10:53 CEST 2012


Dear R-help

Î would like to know if there is a short solution in R for this
merging problem...

Let say I have a dataset A as:

TYPE   DATE
A            2
A            5
A            20
B            10
B            2

(there can be duplicates for the same type and date)

and I have another dataset B as :

TYPE  Special_Date
A              2
A              6
A              20
A              22
B              5
B              6

The question is : I would like to obtain the difference between the
date of each observation in A and the closest special date in B with
the same type. In case of ties I would take the latest date of the
two.

For example I would obtain here

TYPE   DATE   Difference
A            2            0=2-2
A            5            -1=5-6
A            20            0=20-20
B            10           +4=10-6
B            2             -3=2-5

Do you know how to (simply?) obtain this in R?

Many thanks!
Best Regards



More information about the R-help mailing list