[R] fuzzy merge

Alberto Monteiro albmont at centroin.com.br
Wed Apr 9 14:48:07 CEST 2008

ravi wrote:
> d3<-merge(d1,d2,by.x=time1,by.y=time2)
> will work only for exact matching. One possible option is to match 
> the times for the date and hour times only (by filtering away the 
> minute data). But this is only a partial solution as I am not 
> interested in data where the time difference is more than 15 minutes.
Why don't you, in the first "pass", round each time in d2 to
a corresponding date in d1:

new.d2 <- d2
# there may be a faster solution without loops here
for (i in 1:lenght(new.d2)) { # or whatever structure is d2
  for (j in 1:length(d1)) {  # again
    delta.t <- abs(new.dt2[i]$time2 - d1$time1)  # get a vector of deltat's
    j.min <- which.min(delta.t)
    if (delta.t[j.min] <= 15 minutes)
      new.d2[i]$time1 <- d1[j.min]$time1
# and now merge them
d3<-merge(d1, d2, by.x=time1, by.y=time1)

Alberto Monteiro

More information about the R-help mailing list