簡體   English   中英

計算同一 data.frame 中兩個長/緯度點之間的距離

[英]Calculating the distance between two long/lat points in the same data.frame

首先,這是我正在使用的表結構:

df <- structure(list(customer_id = c(353808874L, 69516747L, 357032052L, 
307735090L, 307767260L), id = c("8474", "8107", 
"1617436", "7698", "1617491"), lon1 = c(-115.032623, -115.155029, 
-115.270386, -115.19426, -115.177589), lat1 = c(36.0437202, 36.1366234, 
36.1678734, 36.2635803, 36.2218285), lon2 = c(-115.037022076035, 
-115.150112230012, -115.27341017806, -115.193072645577, -115.174902476442
), lat2 = c(36.0410001245783, 36.141137860928, 36.1700923382169, 
36.2682687632778, 36.2240270452917)), row.names = c(NA, 5L), class = "data.frame")

我嘗試使用來自幾個不同 Stack Overflow 問題的信息,但沒有一個達到預期的結果。

其中之一是這樣的:

earthDist <- function (lon1, lat1, lon2, lat2){
    rad <- pi/180
    a1 <- lat1 * rad
    a2 <- lon1 * rad
    b1 <- lat2 * rad
    b2 <- lon2 * rad
    dlon <- b2 - a2
    dlat <- b1 - a1
    a <- (sin(dlat/2))^2 + cos(a1) * cos(b1) * (sin(dlon/2))^2
    c <- 2 * atan2(sqrt(a), sqrt(1 - a))
    R <- 6378.145
    d <- R * c
    return(d)
}

earthDist(lon[1], lat[1], lon, lat)

但我無法讓它產生我正在尋找的輸出。 我當然不喜歡它,所以如果有人有更有效的東西,我會全神貫注!

編輯:我的預期結果相當簡單。 就三列, distance_between代表 lon/lat1 和 lon/lat2 之間的距離:

+-------------+----+------------------+
| customer_id | id | distance_between |
+-------------+----+------------------+

使用distGeo包中的distGeo函數(類似於上面的函數)可以輕松解決這個問題:

library(geosphere)
#calculate distances in meters
df$distance<-distGeo(df[,c("lon1", "lat1")], df[,c("lon2", "lat2")])

#remove columns
df[, -c(3:6)]

customer_id      id distance
1   353808874    8474 498.2442
2    69516747    8107 668.4088
3   357032052 1617436 366.9541
4   307735090    7698 531.0785
5   307767260 1617491 343.3051

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM