简体   繁体   English

计算R中同一行的2个坐标之间的距离

[英]Calculate the distance between 2 coordinates in the same row in R

I have a data frame [df] like this:我有一个这样的数据框 [df]:

df<-structure(list(  Latitude = c(-23.8, -23.8, -23.9, -23.9), 
                     Longitude = c(-49.6, -49.3, -49.4, -49.8), 
                     Latitude1 = c(-23.4, -23.7, -23.4, -23.8), 
                     Longitude1 = c(-49.7, -49.4, -49.6, -49.7)),
                     class = "data.frame", row.names = c(NA, -4L))

The dataframe contains the GPS coordinates of 2 points and I would like to calculate the distance in meters between these 2 points in every row. dataframe 包含 2 个点的 GPS 坐标,我想计算每行中这 2 个点之间的距离(以米为单位)。 I would only like to get the distance between the 2 points in each row, not a distance matrix.我只想获得每行中 2 个点之间的距离,而不是距离矩阵。

The desired result would look like this:期望的结果如下所示:

Latitude   Longitude   Latitude1   Longitude1   Distance_m
-23.8      -49.6       -23.4       -49.7        53

I tried the geosphere package, but I was not able to get the right results.我尝试了 geosphere package,但我无法获得正确的结果。

Is there any way of doing this, please?请问有什么办法可以吗?

Thank you for any suggestions.感谢您的任何建议。

Check the distm function from geosphere package:从地geosphere package 检查distm function:

apply(df, 1, function(x)distm(c(x[1],x[2]),c(x[3],x[4]),fun = distGeo))

withing the tidyverse // could not reproduce your desired output of 53 though... ?使用tidyverse // 无法重现您想要的 53 的 output ......?

library(geosphere)
library(tidyverse)
df %>%
  mutate(distance = pmap(list(a = Longitude, 
                              b = Latitude, 
                              x = Longitude1,
                              y = Latitude1), 
                          ~ geosphere::distGeo( c(..1, ..2), c(..3, ..4))))

#   Latitude Longitude Latitude1 Longitude1 distance
# 1    -23.8     -49.6     -23.4      -49.7 45461.49
# 2    -23.8     -49.3     -23.7      -49.4 15053.19
# 3    -23.9     -49.4     -23.4      -49.6 59016.34
# 4    -23.9     -49.8     -23.8      -49.7 15048.01

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

相关问题 计算 R 中坐标之间的距离 - Calculate distance between coordinates in R Distm function 用于计算 R 中坐标之间的距离 - Distm function for calculate distance between coordinates in R 如何计算R中低于某个阈值的2个坐标之间的距离? - How to calculate distance between 2 coordinates below a certain threshold in R? 与在excel中计算相同事​​物时,在R中使用distm函数计算两个坐标之间的距离可得出不同的答案 - Using distm function in R to calculate distance between two coordinates gives a different answer than when calculating the same thing in excel 如何使用 R 计算两组点(坐标)之间的欧氏距离的房屋距离 - How to calculate House distance with euclidean distance between two set of points (coordinates) with R 计算坐标R之间的距离 - Calculating the distance between coordinates R R:计算分组数据帧的第一行和当前行之间的距离 - R: Calculate distance between first and current row of grouped dataframe 计算 r 中相同项目的两次出现之间的距离 - calculate distance between two occurrences of the same item in r 如何在R中同时计算多个人之间的地理距离 - How to calculate geographical distance between multiple individuals at same timestep in R 计算坐标之间的距离并插入shiny - Calculate the distance between coordinates and insert in shiny
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM