在2组日期点之间寻找最近的邻居 [英] Finding nearest neighbor between 2 sets of dated points

查看:157
本文介绍了在2组日期点之间寻找最近的邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组点,set1set2.两组点都有与该点关联的数据. set1中的点是短暂的",仅在给定日期存在. set2中的点是永久的",在给定的日期构造,然后在该日期之后永远存在.

I have 2 sets of points, set1 and set2. Both sets of points have a data associated with the point. Points in set1 are "ephemeral", and only exist on the given date. Points in set2 are "permanent", are constructed at a given date, and then exist forever after that date.

set.seed(1)
dates <- seq(as.Date('2011-01-01'),as.Date('2011-12-31'),by='days')

set1 <- data.frame(lat=40+runif(10000),
lon=-70+runif(10000),date=sample(dates,10000,replace=TRUE))

set2 <- data.frame(lat=40+runif(100),
lon=-70+runif(100),date=sample(dates,100,replace=TRUE))

这是我的问题:对于set1中的每个点(短暂),找到到set1事件发生之前构造的set2中最近的点(永久)的距离.例如,set1中的第一个点发生在2011-03-18:

Here's my problem: For each point in set1 (ephemeral) find the distance to the closest point in set2 (permanent) that was constructed BEFORE the event is set1 occurred. For example, the 1st point in set1 occurred on 2011-03-18:

> set1[1,]
       lat       lon       date
1 40.26551 -69.93529 2011-03-18

所以我想在set2中找到在2011-03-18之前构建的最接近的点:

So I want to find the closest point in set2 that was constructed before 2011-03-18:

> head(set2[set2$date<=as.Date('2011-04-08'),])
        lat       lon       date
1  40.41531 -69.25765 2011-02-18
7  40.24690 -69.29812 2011-02-19
13 40.10250 -69.52515 2011-02-12
14 40.53675 -69.28134 2011-02-27
17 40.66236 -69.07396 2011-02-17
20 40.67351 -69.88217 2011-01-04

另外的皱纹是这些是纬度/经度点,因此我必须计算沿地球表面的距离. R包字段提供了

The additional wrinkle is that these are latitude/longitude points, so I have to calculate distances along the surface of the earth. The R package fields provides a convienent function to do this:

require(fields)
distMatrix <- rdist.earth(set1[,c('lon','lat')], 
set2[,c('lon','lat')], miles = TRUE)

我的问题是,如果set2中的点(距离矩阵的列)是在set1中的点(距离矩阵的行)之后构造的,那么如何将该矩阵中的距离调整为Inf?

My question is, how can I adjust the distances in this matrix to Inf if the point in set2 (column of distance matrix) was constructed after the point in set1 (row of distances matrix)?

推荐答案

这就是我要做的事情:

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

这篇关于在2组日期点之间寻找最近的邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆