My question is basically the same as this: calculating distance between two row in a data.table but I am looking for the answer using data.table syntax, not a for loop.
I have a data.table like this:
Lat Lon Time Bus
52.21808 20.96675 2018-04-20 21:27:26 3
52.25882 20.89850 2018-04-20 21:27:23 8
52.24347 21.08460 2018-04-20 21:27:27 1
52.21935 20.97186 2018-04-20 21:28:31 3
52.25808 20.89790 2018-04-20 21:28:32 8
52.24541 21.08522 2018-04-20 21:28:36 1
I want to calculate the distance between two consecutive points, grouping by Bus, using e.g. distGeo from the geosphere package. So something like:
d[,distance:=distGeo(c(Lon, Lat), ???????),by=Bus]
EDIT I get somewhat useful results using
d[,distance:=distGeo(cbind(Lon, Lat)),by=Bus]
but not completely correct: there is a warning that one item for each group needs to be recycled. Is there a way to get NA in either the first or the last row for each bus?
EDIT 2 Looks like I have it.
d[,distance:=c(distGeo(cbind(Lon, Lat)),NA) ,by=Bus]