Finding nearest neighbor between 2 sets of dated points

↘锁芯ラ 提交于 2019-12-06 15:46:48

Here is what I would do:

earlierMatrix <- outer(set1$date, set2$date, "<=")
distMatrix2 <- distMatrix + ifelse(earlierMatrix, Inf, 0)

Here's my attempt at an answer. It's not particularly efficient, but I think it is correct. It also allows you to easily sub in different distance calculators:

#Calculate distances 
require(fields)
distMatrix <- lapply(1:nrow(set1),function(x) {

    #Find distances to all points
    distances <- rdist.earth(set1[x,c('lon','lat')], set2[,c('lon','lat')], miles = TRUE)

    #Set distance to Inf if the set1 point occured BEFORE the set2 dates
    distances <- ifelse(set1[x,'date']<set2[,'date'], Inf, distances)

    return(distances)
})
distMatrix  <- do.call(rbind,distMatrix)

#Find distance to closest object
set1$dist <- apply(distMatrix,1,min)

#Find id of closest object
objectID <- lapply(1:nrow(set1),function(x) {
    if (set1[x,'dist']<Inf) {
        IDs <- which(set1[x,'dist']==distMatrix[x,])
    } else {
        IDs <- NA
    }
    return(sample(IDs,1)) #Randomly break ties (if there are any)
})
set1$objectID <- do.call(rbind,objectID)

Here's the head of the resulting dataset:

> head(set1)
       lat       lon       date      dist objectID
1 40.26551 -69.93529 2011-03-18  3.215514       13
2 40.37212 -69.32339 2011-02-11 10.320910       46
3 40.57285 -69.26463 2011-02-23  3.954132        4
4 40.90821 -69.88870 2011-04-24  4.132536       49
5 40.20168 -69.95335 2011-02-24  4.284692       45
6 40.89839 -69.86909 2011-07-12  3.385769       57
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!