简体   繁体   English

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

[英]R calculate distance in miles using 2 latitude & 2 longitude vectors in a data frame for 18k rows

I have a data frame with 6 columns:我有一个包含 6 列的数据框:

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

I would like to calculate the distance between each of the 2 points in miles and add it as a new column.我想计算两个点之间的距离(以英里为单位)并将其添加为新列。 I'm struggling to find a function that does this.我正在努力寻找能够做到这一点的 function。 The closest I found is here: https://blog.exploratory.io/calculating-distances-between-two-geo-coded-locations-358e65fcafae but it fails because it can't find a function called ' list_extract '.我找到的最接近的是这里: https://blog.exploratory.io/calculating-distances-between-two-geo-coded-locations-358e65fcafae但它失败了,因为它找不到名为'list_extract的list_extract '。

Here's a sample of the data:以下是数据示例:

structure(list(df1_location_number = c(5051, 5051, 5051, 5051, 
5051), df1_Latitude = c(34.7171375, 34.7171375, 34.7171375, 34.7171375, 
34.7171375), df1_Longitude = c(-118.9107316, -118.9107316, -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")

Any suggestions?有什么建议么?

library(geodist) is a good & fast library for calculating distances, and the geodist_vec() function is vectorised to work on 'columns' of data library(geodist)是一个用于计算距离的好且快速的库,并且geodist_vec() function 被矢量化以处理数据的“列”

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


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用纬度和经度向量在R中查找步行距离 - Find walking distance in R using vectors of latitude and longitude 计算数据帧R中多个距离经度纬度 - Calculate distance longitude latitude of multiple in dataframe R 如何将R中的纬度(行)x经度(列)数据框更改为纬度(第1列),经度(第2列),值(第3列)数据框? - How to change a latitude (rows) x longitude (columns) data frame into a latitude (column 1), longitude (column 2), value (column 3) data frame in R? R根据两个数据帧的经纬度计算距离 - R calculate distance based on latitude-longitude from two data frames 无法使用Haversine公式正确使用R中的纬度和经度计算距离 - Unable to calculate the distance using latitude and longitude in R using Haversine formula correctly 如何使用R计算以英里为单位的两个邮政编码之间的距离 - How to calculate Distance between Two Zipcodes in Miles Using R 使用具有循环的不同长度的不同数据帧中的纬度和经度数据计算距离 - Calculate Distance using Latitude and Longitude data in Different Data frames of different lengths with loop 如何计算多个经纬度数据之间的距离? - How can I calculate distance between multiple latitude and longitude data? 获取经度点和纬度点向量之间的距离 - Getting distance between vectors of longitude and latitude points 计算纬度和经度点之间的距离? - Calculate distance between latitude and longitude points?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM