我对https://github.com/michaelmalick/r-malick/blob/master/R/haversine.R上的 Haversine 函数进行了矢量化
haversine <- function(lon1, lat1, lon2, lat2, r = 6378137) {
if(!is.numeric(c(lon1, lat1, lon2, lat2)))
stop("Inputs are not numeric")
# Convert degrees to radians
lon1 <- lon1 * pi / 180
lat1 <- lat1 * pi / 180
lon2 <- lon2 * pi / 180
lat2 <- lat2 * pi / 180
delta.lon <- (lon2 - lon1)
delta.lat <- (lat2 - lat1)
a <- sin(delta.lat/2)^2 + cos(lat1) * cos(lat2) *
sin(delta.lon/2)^2
c <- 2 * asin(min(1,sqrt(a)))
d <- r * c
return(d) # Distance
}
vectorized_haversine <- Vectorize(haversine, vectorize.args = c("lon1", "lat1", "lon2", "lat2"))
接下来,我使用了 dplyr 函数 'lag' 和 'mutate',以及矢量化 hasrsine 函数来获取连续点之间的距离(函数 'tribble' 用于重新创建head(datagps)
)。
library(dplyr)
tribble(
~`Date & Time [Local]`, ~Latitude, ~Longitude,
"2018-06-18 03:000", -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
) %>%
mutate(Step =
vectorized_haversine(Longitude, Latitude, lag(Longitude), lag(Latitude)))
Date & Time [Local] Latitude Longitude Step
1 2018-06-18 03:000 -2.434901 34.85359 NA
2 2018-06-18 03:06:00 -2.434598 34.85387 45.90731
3 2018-06-18 03:08:00 -2.434726 34.85382 15.29559
4 2018-06-18 03:12:00 -2.434816 34.85371 15.81292
5 2018-06-18 03:16:00 -2.434613 34.85372 22.62521
6 2018-06-18 03:20:00 -2.434511 34.85376 12.19500