[英]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))
dataframe 包含 2 個點的 GPS 坐標,我想計算每行中這 2 個點之間的距離(以米為單位)。 我只想獲得每行中 2 個點之間的距離,而不是距離矩陣。
期望的結果如下所示:
Latitude Longitude Latitude1 Longitude1 Distance_m
-23.8 -49.6 -23.4 -49.7 53
我嘗試了 geosphere package,但我無法獲得正確的結果。
請問有什么辦法可以嗎?
感謝您的任何建議。
從地geosphere
package 檢查distm
function:
apply(df, 1, function(x)distm(c(x[1],x[2]),c(x[3],x[4]),fun = distGeo))
使用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.