计算R中同一行中2个坐标之间的距离
Calculate the distance between 2 coordinates in the same row in R
我有一个这样的数据框[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))
数据框包含 2 个点的 GPS 坐标,我想计算每行中这 2 个点之间的距离(以米为单位)。我只想获取每行中 2 个点之间的距离,而不是距离矩阵。
期望的结果如下所示:
Latitude Longitude Latitude1 Longitude1 Distance_m
-23.8 -49.6 -23.4 -49.7 53
我尝试了 geosphere 包,但无法获得正确的结果。
请问有什么办法吗?
感谢您的任何建议。
检查 geosphere
包中的 distm
函数:
apply(df, 1, function(x)distm(c(x[1],x[2]),c(x[3],x[4]),fun = distGeo))
虽然 tidyverse
// 无法重现您想要的输出 53... ?
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
我有一个这样的数据框[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))
数据框包含 2 个点的 GPS 坐标,我想计算每行中这 2 个点之间的距离(以米为单位)。我只想获取每行中 2 个点之间的距离,而不是距离矩阵。
期望的结果如下所示:
Latitude Longitude Latitude1 Longitude1 Distance_m
-23.8 -49.6 -23.4 -49.7 53
我尝试了 geosphere 包,但无法获得正确的结果。
请问有什么办法吗?
感谢您的任何建议。
检查 geosphere
包中的 distm
函数:
apply(df, 1, function(x)distm(c(x[1],x[2]),c(x[3],x[4]),fun = distGeo))
虽然 tidyverse
// 无法重现您想要的输出 53... ?
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