计算R中同一行中两个坐标之间的距离
我有这样一个数据帧[df]:计算R中同一行中两个坐标之间的距离,r,dataframe,sp,geosphere,R,Dataframe,Sp,Geosphere,我有这样一个数据帧[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(-4
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))
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
谢谢你,但是我总是会遇到这样的错误:在.pointToMatrix(x):经度<-360中出错。我的经度是数字格式的,没有一个真的低于-360。奇怪。。我无法重现这个错误。。。