微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R 使用 18k 行数据框中的 2 个纬度和 2 个经度向量计算以英里为单位的距离

如何解决R 使用 18k 行数据框中的 2 个纬度和 2 个经度向量计算以英里为单位的距离

我有一个包含 6 列的数据框:

- location id 1
- latitude 1
- longitude 1
- location id 2
- latitude 2
- longitude 2

我想以英里为单位计算 2 个点中的每一个间的距离,并将其添加为新列。我正在努力寻找执行此操作的函数。我找到的最接近的是:https://blog.exploratory.io/calculating-distances-between-two-geo-coded-locations-358e65fcafae 但它失败了,因为它找不到名为“list_extract”的函数

以下是数据示例:

structure(list(df1_location_number = c(5051,5051,5051),df1_Latitude = c(34.7171375,34.7171375,34.7171375),df1_Longitude = c(-118.9107316,-118.9107316,-118.9107316),df2_location_number = c(3051,3085,3022,3041,3104),df2_Latitude = c(34.7171375,39.53404,31.626788,35.247982,39.33425),df2_Longitude = c(-118.9107316,-93.292373,-88.330116,-84.804119,-123.713064)),row.names = c(NA,5L),class = "data.frame")

有什么建议吗?

解决方法

library(geodist) 是一个很好且快速的计算距离的库,geodist_vec() 函数被矢量化以处理数据的“列”

library(geodist)

## calcualte distance in metres using Haversine formula
df$dist_m <- geodist::geodist_vec(
  x1 = df$df1_Longitude,y1 = df$df1_Latitude,x2 = df$df2_Longitude,y2 = df$df2_Latitude,paired = TRUE,measure = "haversine"
)

## convert to miles
df$dist_miles <- df$dist_m / 1609

#   df1_location_number df1_Latitude df1_Longitude df2_location_number df2_Latitude df2_Longitude    dist_m dist_miles
# 1                5051     34.71714     -118.9107                3051     34.71714    -118.91073       0.0     0.0000
# 2                5051     34.71714     -118.9107                3085     39.53404     -93.29237 2327593.8  1446.6089
# 3                5051     34.71714     -118.9107                3022     31.62679     -88.33012 2859098.6  1776.9413
# 4                5051     34.71714     -118.9107                3041     35.24798     -84.80412 3095858.6  1924.0886
# 5                5051     34.71714     -118.9107                3104     39.33425    -123.71306  667849.7   415.0713


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。