当两个数据框的行数不同时,如何计算一个数据框中每个位置与另一个数据框中每个位置之间的距离?
例如,假设我有一个包含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,没有任何有用的信息。