I have 2 data frames: 1 is a sequence of GPS locations with associated date-times (POSIXct)
GPS <- data.frame(Lat=c(58.65209, 58.65183, 58.65142, 58.65129, 58.65126, 58.65124, 58.65122, 58.65119, 58.65117, 58.65115),
Lon=c(-3.178559, -3.177934, -3.177277, -3.177536, -3.177494, -3.177713, -3.177806, -3.177899, -3.177991, -3.178084),
datetime=c("2016-10-01 16:23:59 GMT", "2016-10-01 16:31:59 GMT", "2016-10-01 16:39:59 GMT", "2016-10-01 16:47:59 GMT", "2016-10-01 16:55:59 GMT", "2016-10-01 17:03:59 GMT", "2016-10-01 17:11:59 GMT", "2016-10-01 17:19:59 GMT", "2016-10-01 17:27:59 GMT", "2016-10-01 17:35:59 GMT"))
GPS$datetime <- as.POSIXct(as.character(GPS$datetime))
and another is a sequence of depths with associated date-times (POSIXct).
DEPTH <- data.frame(Depth=c(0.0, 0.1, 0.0, 0.0, 0.1, 1.5, 1.7, 1.7, 1.4, 1.5, 1.8, 2.1, 2.3, 1.7, 2.0, 2.6, 2.2, 2.1, 3.4, 3.3),
datetime=c("2016-10-01 16:22:56 GMT", "2016-10-01 16:23:06 GMT", "2016-10-01 16:23:16 GMT", "2016-10-01 16:23:59 GMT", "2016-10-01 16:24:52 GMT", "2016-10-01 16:25:24 GMT", "2016-10-01 16:32:40 GMT", "2016-10-01 16:32:51 GMT", "2016-10-01 18:45:30 GMT", "2016-10-01 18:45:40 GMT", "2016-10-01 18:46:54 GMT", "2016-10-01 18:47:04 GMT", "2016-10-01 18:47:14 GMT", "2016-10-01 18:47:25 GMT", "2016-10-01 18:51:03 GMT", "2016-10-01 18:51:14 GMT", "2016-10-01 18:51:24 GMT", "2016-10-01 18:54:11 GMT", "2016-10-01 18:54:21 GMT", "2016-10-01 18:54:32 GMT"))
DEPTH$datetime <- as.POSIXct(as.character(DEPTH$datetime))
For each depth location I want to assign a location (latitude and longitude) based on when the interpolated track from the location data frame suggests it should be i.e. if the locations go from point A to point B at which point along that line does the depth data lie, assuming a uniform speed between points, given it's date-time.
the final product would be 2 vectors in the data-frame which assign each depth value with a latitude and a longitude.
Thank you.
Assuming constant velocity (uniform speed and constant direction) between successive way-points in the GPS
data, we can perform linear interpolation separately for Lat
and Lon
using approx
from stats
:
DEPTH$Lat <- approx(x=GPS$datetime, y=GPS$Lat, xout=DEPTH$datetime, method="linear")$y
DEPTH$Lon <- approx(x=GPS$datetime, y=GPS$Lon, xout=DEPTH$datetime, method="linear")$y
In this usage, interpolated values for datetime
in DEPTH
outside of the range of datetime
in GPS
are assigned NA
. See ?approx
for other ways to dealing with interpolating outside the range of the input.
Using your data, the result is:
print(DEPTH)
## Depth datetime Lat Lon
##1 0.0 2016-10-01 16:22:56 NA NA
##2 0.1 2016-10-01 16:23:06 NA NA
##3 0.0 2016-10-01 16:23:16 NA NA
##4 0.0 2016-10-01 16:23:59 58.65209 -3.178559
##5 0.1 2016-10-01 16:24:52 58.65206 -3.178490
##6 1.5 2016-10-01 16:25:24 58.65204 -3.178448
##7 1.7 2016-10-01 16:32:40 58.65179 -3.177878
##8 1.7 2016-10-01 16:32:51 58.65179 -3.177863
##9 1.4 2016-10-01 18:45:30 NA NA
##10 1.5 2016-10-01 18:45:40 NA NA
##11 1.8 2016-10-01 18:46:54 NA NA
##12 2.1 2016-10-01 18:47:04 NA NA
##13 2.3 2016-10-01 18:47:14 NA NA
##14 1.7 2016-10-01 18:47:25 NA NA
##15 2.0 2016-10-01 18:51:03 NA NA
##16 2.6 2016-10-01 18:51:14 NA NA
##17 2.2 2016-10-01 18:51:24 NA NA
##18 2.1 2016-10-01 18:54:11 NA NA
##19 3.4 2016-10-01 18:54:21 NA NA
##20 3.3 2016-10-01 18:54:32 NA NA