[英]Calculating distance between two points using the distm function inside mutate
[英]Calculating distance between two GPS locations in a data frame using distm () in R
之前已經問過這個問題,但從未對數據進行如下安排。 以下是它的一個示例:
> head(datagps)
Date & Time [Local] Latitude Longitude
1: 2018-06-18 03:01:00 -2.434901 34.85359
2: 2018-06-18 03:06:00 -2.434598 34.85387
3: 2018-06-18 03:08:00 -2.434726 34.85382
4: 2018-06-18 03:12:00 -2.434816 34.85371
5: 2018-06-18 03:16:00 -2.434613 34.85372
6: 2018-06-18 03:20:00 -2.434511 34.85376
如您所見,我有一個Date & Time [Local]
列,平均每4分鍾注冊一次GPS位置。 我想計算兩個連續錄音之間的距離(以米為單位),並將此測量值存儲在新的列Step
。 我一直在嘗試對我的數據實現distm()
:
> datagps$Step<-distm(c(datagps$Longitude, datagps$Latitude), c(datagps$Longitude+1, datagps$Latitude+1), fun = distHaversine)
Error in .pointsToMatrix(x) : Wrong length for a vector, should be 2
雖然我對語法非常不確定,如果這是填充函數參數的正確方法。 我對R很新,所以我希望能得到一些幫助。
任何輸入都表示贊賞!
我想你已經差不多了。 假設您想要存儲前一個記錄( n
)和當前記錄( n+1
)之間的距離n+1
,您可以使用:
library(geosphere)
date <- c("2018-06-18 03:01.00","2018-06-18 03:06.00","2018-06-18 03:08.00","2018-06-18 03:12.00","2018-06-18 03:16.00","2018-06-18 03:20.00")
latitude <- c(-2.434901,-2.434598,-2.434726,-2.434816,-2.434613,-2.434511)
longitude <- c(34.85359,34.85387,34.85382,34.85371,34.85372,34.85376)
datagps <- data.frame(date,lat,lon)
datagps$length <- distm(x=datagps[,2:3], fun = distHaversine)[,1]
這給出了第一個結果0,其余為連續點之間的距離
如果您查看函數的文檔,您將看到:
library(geosphere)
?distm
x經度/緯度(點)。 可以是兩個數字的向量,2列的矩陣(第一個是經度,第二個是緯度)或SpatialPoints *對象
y與x相同。 如果缺少,y與x相同
這意味着您可以同時使用矩陣或向量。
一種方法可能是:
res <- distm(as.matrix(df1[,c("Longitude","Latitude")]), fun = distHaversine)
res
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 0.00000 45.90731 32.15371 16.36018 35.16947 47.35305
#[2,] 45.90731 0.00000 15.29559 30.09289 16.76621 15.60347
#[3,] 32.15371 15.29559 0.00000 15.81292 16.79079 24.84658
#[4,] 16.36018 30.09289 15.81292 0.00000 22.62521 34.40483
#[5,] 35.16947 16.76621 16.79079 22.62521 0.00000 12.19500
#[6,] 47.35305 15.60347 24.84658 34.40483 12.19500 0.00000
使用sf
-package的解決方案
樣本數據
library(data.table)
dt1 <- data.table::fread( 'DateTime, Latitude, Longitude
2018-06-18 03:01:00, -2.434901, 34.85359
2018-06-18 03:06:00, -2.434598, 34.85387
2018-06-18 03:08:00, -2.434726, 34.85382
2018-06-18 03:12:00, -2.434816, 34.85371
2018-06-18 03:16:00, -2.434613, 34.85372
2018-06-18 03:20:00, -2.434511, 34.85376')
setDF(dt1)
碼
library(sf)
#create spatial points object
dt1.sf <- st_as_sf( x= dt1,
coords = c("Longitude", "Latitude"),
crs = "+proj=longlat +datum=WGS84")
#calculate distances
st_distance(dt1.sf)
產量
# Units: [m]
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 0.00000 45.74224 32.07520 16.32379 34.97450 47.08749
# [2,] 45.74224 0.00000 15.20702 29.96245 16.76520 15.56348
# [3,] 32.07520 15.20702 0.00000 15.77068 16.72801 24.69270
# [4,] 16.32379 29.96245 15.77068 0.00000 22.47452 34.18116
# [5,] 34.97450 16.76520 16.72801 22.47452 0.00000 12.12446
# [6,] 47.08749 15.56348 24.69270 34.18116 12.12446 0.00000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.