My df looks like this:
bid ts latitude longitude
1 827566 1999-10-07 42.40944 -88.17822
2 827566 2013-04-11 41.84740 -87.63126
3 1902966 201
I think you're overcomplicating it a little. I wish geosphere::distHaversine
had a slightly more intuitive calling method (similar to, say, diff
), but it's not hard to work around it:
dat <- read.table(text = " bid ts latitude longitude
827566 1999-10-07 42.40944 -88.17822
827566 2013-04-11 41.84740 -87.63126
1902966 2012-05-02 45.52607 -94.20649
1902966 2013-03-25 41.94083 -87.65852
3211972 2012-08-14 43.04786 -87.96618
3211972 2013-08-02 41.88258 -87.63760", header = TRUE, stringsAsFactors = FALSE)
dat$ts <- as.Date(dat$ts)
library(dplyr)
library(geosphere)
group_by(dat, bid) %>%
mutate(
d = c(NA,
distHaversine(cbind(longitude[-n()], latitude[-n()]),
cbind(longitude[ -1], latitude[ -1]))),
dts = c(NA, diff(ts))
) %>%
ungroup() %>%
filter( ! is.na(d) )
# # A tibble: 3 × 6
# bid ts latitude longitude d dts
# <int> <date> <dbl> <dbl> <dbl> <dbl>
# 1 827566 2013-04-11 41.84740 -87.63126 77159.35 4935
# 2 1902966 2013-03-25 41.94083 -87.65852 660457.41 327
# 3 3211972 2013-08-02 41.88258 -87.63760 132494.65 353