R中不同位置之间的距离(纬度和经度作为输入)

3

当两个数据框的行数不同时,如何计算一个数据框中每个位置与另一个数据框中每个位置之间的距离?

例如,假设我有一个包含3行的数据框 A 和一个包含4行的数据框 B。这些数据来自 SNAP(美国联邦政府为低收入家庭提供食品/饮料援助)零售商定位器

A
      Store_Name     Longitude Latitude
1     Food Lion 2213 -80.86581 35.59477
2 THE CORNER GROCERY -81.09917 35.26776
3          FISH WING -80.88245 35.21639


B
         Store_Name     Longitude Latitude
1    SUPERIOR GROCERIES -79.80839 35.73597
2     MORVEN TRUCK STOP -80.01122 34.88312
3       GREENHILL STORE -81.99146 35.34768
4 NORTHSIDE FOOD MARKET -77.94242 34.24158

以下是一些失败的尝试:

mapdist(as.character(a), as.character(b)) </code>

YIELDS: 返回类似以下条目的from和to,并且只有一个可能有用的距离计算:c(35.594765, 35.267761, 35.216393)

distcomp <- mapdist(from = c(lon = as.character(a$Longitude), lat = as.character(a$Latitude)), to = c(lon = as.character(b$Longitude),   lat = as.character(b$Latitude)), mode = "driving")

结果: 错误 <code> 参数表明行数不同:6、8

# row-bind the rows even though this would mean extra work so that I could only have the distances from those in <code>a</code> to those in <code>b</code>:

c <- rbind(a,b)
distcomp <- mapdist(from = c(lon = as.character(c$Longitude), lat = as.character(c$Latitude)), 
                     to = c(lon = as.character(c$Longitude),   lat = as.character(c$Latitude)), mode = "driving")

产量:结果中有一堆NA,没有任何有用的信息。
1个回答

3
你可以将这两张表合并起来,以便每个表的每一行都与另一个表的每一行连接(通常称为全连接),然后计算每组经纬度坐标之间的距离。
在你的问题中,似乎你正在询问 Google 地图提供的驾车距离。这里我会同时提供驾车和 Haversine 距离。

数据连接

## create a key column to join on
A$key <- 1
B$key <- 1

## Join together
AB <- merge(A, B, by = "key")

Distance Calculation - haversine

## Here I'm using the haversine distance calculation from geosphere
library(geosphere)

AB$distance <- distHaversine(p1 = matrix(c(AB$Longitude.x, AB$Latitude.x), ncol = 2),
                             p2 = matrix(c(AB$Longitude.y, AB$Latitude.y), ncol = 2))


head(AB)
#   key     Store_Name.x Longitude.x Latitude.x        Store_Name.y Longitude.y Latitude.y  distance
# 1   1     FoodLion2213   -80.86581   35.59477   SUPERIORGROCERIES   -79.80839   35.73597  96915.65
# 2   1     FoodLion2213   -80.86581   35.59477     MORVENTRUCKSTOP   -80.01122   34.88312 110963.56
# 3   1     FoodLion2213   -80.86581   35.59477      GREENHILLSTORE   -81.99146   35.34768 105691.88
# 4   1     FoodLion2213   -80.86581   35.59477 NORTHSIDEFOODMARKET   -77.94242   34.24158 306403.99
# 5   1 THECORNERGROCERY   -81.09917   35.26776   SUPERIORGROCERIES   -79.80839   35.73597 128061.51
# 6   1 THECORNERGROCERY   -81.09917   35.26776     MORVENTRUCKSTOP   -80.01122   34.88312 107968.35

距离计算- Google地图驾驶

要使用Google地图距离,您可以像以前一样使用ggmap中的mapdist(但由于某种原因,我只能在apply函数中使其起作用)。

library(ggmap)

mdist <- apply(AB, 1, function(x){
    f = as.numeric(c(x[['Longitude.x']], x[['Latitude.x']]))
    t = as.numeric(c(x[['Longitude.y']], x[['Latitude.y']]))
    mapdist(f, t)
})

AB$mapDist <- do.call(rbind, mdist)

head(AB)
# key     Store_Name.x Longitude.x Latitude.x        Store_Name.y Longitude.y Latitude.y  distance
# 1   1     FoodLion2213   -80.86581   35.59477   SUPERIORGROCERIES   -79.80839   35.73597  96915.65
# 2   1     FoodLion2213   -80.86581   35.59477     MORVENTRUCKSTOP   -80.01122   34.88312 110963.56
# 3   1     FoodLion2213   -80.86581   35.59477      GREENHILLSTORE   -81.99146   35.34768 105691.88
# 4   1     FoodLion2213   -80.86581   35.59477 NORTHSIDEFOODMARKET   -77.94242   34.24158 306403.99
# 5   1 THECORNERGROCERY   -81.09917   35.26776   SUPERIORGROCERIES   -79.80839   35.73597 128061.51
# 6   1 THECORNERGROCERY   -81.09917   35.26776     MORVENTRUCKSTOP   -80.01122   34.88312 107968.35
# mapDist.from                                       mapDist.to mapDist.m
# 1 507-543 NC-150, Mooresville, NC 28117, USA 1504N N Fayetteville St, Asheboro, NC 27203, USA    118648
# 2 507-543 NC-150, Mooresville, NC 28117, USA                2876 US-52, Morven, NC 28119, USA    135294
# 3 507-543 NC-150, Mooresville, NC 28117, USA   136 Firethorn Ln, Rutherfordton, NC 28139, USA    149211
# 4 507-543 NC-150, Mooresville, NC 28117, USA      603 Red Cross St, Wilmington, NC 28401, USA    359219
# 5  101 McAdenville Rd, Lowell, NC 28098, USA 1504N N Fayetteville St, Asheboro, NC 27203, USA    160581
# 6  101 McAdenville Rd, Lowell, NC 28098, USA                2876 US-52, Morven, NC 28119, USA    137167
# mapDist.km mapDist.miles mapDist.seconds mapDist.minutes mapDist.hours
# 1    118.648      73.72787            5049        84.15000      1.402500
# 2    135.294      84.07169            5574        92.90000      1.548333
# 3    149.211      92.71972            5812        96.86667      1.614444
# 4    359.219     223.21869           13264       221.06667      3.684444
# 5    160.581      99.78503            5782        96.36667      1.606111
# 6    137.167      85.23557            5639        93.98333      1.566389

请注意,我观察到distHaversine提供的距离单位为米,以防有人查看@SymbolixAU的输出并对其计量单位产生疑惑。mapdist明确提供公里(km)和英里的距离。 - Rick Pack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接