R 中不等数量的位置之间的距离(纬度和经度是输入)
Distance between unequal number of locations in R (latitude and longitude are inputs)
当行数不同时,如何计算一个数据框中每个位置与另一个数据框中每个位置之间的距离?
例如,假设我有 3 行的数据框 A 和 4 行的数据框 B。这些来自 SNAP(美国联邦低收入家庭食品/饮料援助)Retailer Locator :
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")
产量:Error <code> arguments imply differing number of rows: 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。没有任何帮助。
您可以将 table 合并在一起,以便每个 table 的每一行都连接到另一个 table 的每一行(通常称为完全连接) , 然后计算每组 lat/lon 坐标之间的距离。
在您的问题中,您似乎在询问 Google 地图给出的行驶距离。在这里,我同时给出了行驶距离和半正弦距离。
加入数据
## create a key column to join on
A$key <- 1
B$key <- 1
## Join together
AB <- merge(A, B, by = "key")
距离计算-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
(但出于某种原因,我只能让它在 [=15= 中工作) ]函数)
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
当行数不同时,如何计算一个数据框中每个位置与另一个数据框中每个位置之间的距离?
例如,假设我有 3 行的数据框 A 和 4 行的数据框 B。这些来自 SNAP(美国联邦低收入家庭食品/饮料援助)Retailer Locator :
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")
产量:Error <code> arguments imply differing number of rows: 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。没有任何帮助。
您可以将 table 合并在一起,以便每个 table 的每一行都连接到另一个 table 的每一行(通常称为完全连接) , 然后计算每组 lat/lon 坐标之间的距离。
在您的问题中,您似乎在询问 Google 地图给出的行驶距离。在这里,我同时给出了行驶距离和半正弦距离。
加入数据
## create a key column to join on
A$key <- 1
B$key <- 1
## Join together
AB <- merge(A, B, by = "key")
距离计算-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
(但出于某种原因,我只能让它在 [=15= 中工作) ]函数)
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