简体   繁体   English

如何找到两个不同数据框之间的最近距离

[英]How to find the nearest distance between two different data frames

I am trying to find the nearest distance of locations in dataset 1 to dataset 2. Both data sets are different sizes. 我正在尝试找到数据集1到数据集2的最接近距离。两个数据集的大小不同。 Ive looked into using the Haversine function but I'm unsure what I need to do after. 我已经研究过使用Haversine函数,但是我不确定之后需要做什么。

Since you have not provided a sample of your data, I am going to use the oregon.tract data set from the UScensus2000tract library as a reproducible example. 由于您尚未提供数据样本,因此我将使用UScensus2000tract库中的oregon.tract数据集作为可重现的示例。

Here is a solution based on fast data.table that I get from this other answer here . 这是基于快速data.table的解决方案,我从这里的其他答案中获得了解决方案。

# load libraries
  library(data.table)
  library(geosphere)
  library(UScensus2000tract)
  library(rgeos)

Now let's create a new data.table with all possible pair combinations of origins (census centroids) and destinations (facilities) 现在,让我们创建一个新的data.table其中包含起点(普查质心)和终点(设施)的所有可能的对组合

# get all combinations of origin and destination pairs
# Note that I'm considering here that the distance from A -> B is equal 
from B -> A.
  odmatrix <- CJ(Datatwo$Code_A , Dataone$Code_B)
  names(odmatrix) <- c('Code_A', 'Code_B') # update names of columns

# add coordinates of Datatwo centroids (origin)
  odmatrix[Datatwo, c('lat_orig', 'long_orig') := list(i.Latitude, 
i.Longitude), on= "Code_A" ]

# add coordinates of facilities (destination)
  odmatrix[Dataone, c('lat_dest', 'long_dest') := list(i.Latitude,  
i.Longitude), on= "Code_B" ]


Now you just need to:

# calculate distances
  odmatrix[ , dist := distHaversine(matrix(c(long_orig, lat_orig), ncol 
= 2), 
                                    matrix(c(long_dest, lat_dest), ncol  
= 2))]

# and get the nearest destinations for each origin
  odmatrix[, .(  Code_B = Code_B[which.min(dist)],
                    dist = min(dist)), 
                                    by = Code_A]

### Prepare data for this reproducible example
# load data
  data("oregon.tract")

# get centroids as a data.frame
  centroids <- as.data.frame(gCentroid(oregon.tract,byid=TRUE))

# Convert row names into first column
  setDT(centroids, keep.rownames = TRUE)[]

# get two data.frames equivalent to your census and facility data 
frames
  Datatwo<- copy(centroids)
  Dataone <- copy(centroids)

  names(Datatwo) <- c('Code_A', 'Longitude', 'Latitude')
  names(Dataone) <- c('Code_B', 'Longitude', 'Latitude')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到两个数据框之间的距离 - How to find the distance between two data frames 使用 sf 通过两个数据帧之间的不同年份计算最近点坐标和距离 - Calculate nearest point coordinate and distance by differing years between two data frames using sf 计算不同数据帧中点之间的距离 - Calculating the distance between points in different data frames 两个大小不等的数据框之间的距离 - Distance between two data frames of unequal size 找到第二个数据帧中每个元素的两个数据帧之间的最小距离 - Find the minimum distance between two data frames, for each element in the second data frame 匹配两个data.frames之间的最近日期 - Matching nearest date between two data.frames 计算两个不同分组数据框中位置点之间的最大距离 - Calculating maximum distance between location points in two different grouped data frames 使用两个数据集查找最近邻居的欧几里得距离 - Find the euclidean distance for nearest neighbor with two data sets 如何根据条件查找两个数据帧之间的重叠区域 - How to find overlapping regions between two data frames based on conditions 我将如何 output 二进制数据帧列表中任意两个 1 之间的欧几里得距离? - How would I output the euclidean distance between any two 1s in a list of binary data frames?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM