简体   繁体   English

计算同一 data.frame 中两个长/纬度点之间的距离

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

For starters, here is the table structure I'm using:首先,这是我正在使用的表结构:

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")

I have attempted using the info from several different Stack Overflow questions, but none have reached the desired outcome.我尝试使用来自几个不同 Stack Overflow 问题的信息,但没有一个达到预期的结果。

One of them was this:其中之一是这样的:

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)

But I could not get it to produce the output I'm looking for.但我无法让它产生我正在寻找的输出。 I'm certainly not attached to it, so if anyone has something more effective, I'm all ears!我当然不喜欢它,所以如果有人有更有效的东西,我会全神贯注!

EDIT: My intended outcome is rather simple.编辑:我的预期结果相当简单。 Just three columns, distance_between representing the distance between lon/lat1 and lon/lat2:就三列, distance_between代表 lon/lat1 和 lon/lat2 之间的距离:

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

This is a easily solved with the distGeo function (similar to your functions above) from geosphere package:使用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.

相关问题 计算数据框中两个经纬度之间的距离 - Calculating Distance Between Two Lat's and Longs Within A Data Frame 从data.frame计算值之间的距离时出错 - Error calculating the distance between values from a data.frame 如何在我的数据框中添加一列来计算具有匹配 ID 的前一个点之间的纬度/经度点之间的距离 - How to add a column to my data frame that calculates the distance between lat/long points between the previous point with matching IDs 从 shapefile 中提取 Long/Lat 并计算两组坐标之间的最近距离 - Extracting Long/Lat from a shapefile and calculating nearest distance between two sets of coordinates 如何计算数据框中两个连续点之间的距离和角度? - How to calculate distance and angle between two consecutive points in data frame? 计算两类拉特隆点之间的距离 - Calculating distance between two classes of latlong points 计算不同数据帧中点之间的距离 - Calculating the distance between points in different data frames 计算数据帧2个元素之间的距离 - Calculating distance between 2 elements of a data frame 计算数据帧中多个经纬度点的中心点 - Calculate a centre point of multiple lat, long points in a data-frame 计算数据框中两个长纬度坐标之间的距离 - Calculate distance between two long lat coordinates in a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM