I have the following dataframe (df) and would like to interpolate Lat, Lon coordinates at an equidistant interval (e.g. every 250 m) or time interval (e.g. every 2 min).
> head(df)
ID Latitude Longitude trip date.time
1 1 10.30447 -109.2323 1 2005-01-07 11:25:26
2 1 10.30425 -109.2321 1 2005-01-07 11:25:36
3 1 10.30314 -109.2326 1 2005-01-07 11:25:46
4 1 10.30199 -109.2328 1 2005-01-07 11:25:56
5 1 10.30079 -109.2334 1 2005-01-07 11:26:06
6 1 10.30006 -109.2331 1 2005-01-07 11:26:16
I tried to do this using R package zoo and the following code I found in a similar question posted:
full.time <- with(df,seq(date.time[1],tail(date.time,1),by=1))
library(zoo)
df.zoo <- zoo(df[,3:4],df$date.time) # convert to zoo object
result <- na.approx(df.zoo,xout=full.time) # interpolate; result is also a zoo object
head(result)
However, as my dataframe includes multiple trips (df$trip) of multiple individuals (df$ID), I get the following error message:
> df.zoo <- zoo(df[,3:4],df$date.time) # convert to zoo object
Warning message:
In zoo(df[, 3:4], df$datetime) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
How can I run above code (in a loop?) accounting for individual trips?