Это звучит как работа для неэквивалентного объединения, используя data.table
!
library( data.table )
пример данных
dt1 <- fread( "Datetime, Latitude, Longitude
2018-10-01 08:27:10, 34.8014080, 103.8499800
2018-09-30 04:55:51, 43.3367432, 44.158934
2018-02-28 17:03:27, 37.0399910, 115.6672080", header = T)
dt2 <- fread("Datetime, Latitude, Longitude
2018-10-01 08:57:10, 34.8014080, 103.8999800
2018-09-30 04:55:51, 43.3367432, 48.158934
2018-02-27 17:03:27, 37.0399910, 115.6672080", header = T)
данные-подготовка
#set datetimes to POSIXct
dt1[, Datetime := as.POSIXct( Datetime, format = "%Y-%m-%d %H:%M:%S") ]
dt2[, `:=`(Datetime = as.POSIXct( Datetime, format = "%Y-%m-%d %H:%M:%S" ) )]
соединение
#create boundaries
dt2[, `:=`(Datetime_max = Datetime + 3600,
Datetime_min = Datetime - 3600,
Latitude_max = Latitude + 0.1,
Latitude_min = Latitude - 0.1,
Longitude_max = Longitude + 0.1,
Longitude_min = Longitude - 0.1) ]
#perform non-equi join
dt1[ dt2, on = .( Datetime <= Datetime_max,
Datetime >= Datetime_min,
Latitude <= Latitude_max,
Latitude >= Latitude_min,
Longitude <= Longitude_max,
Longitude >= Longitude_min ),
nomatch = 0L]
результат
# Datetime Latitude Longitude Datetime.1 Latitude.1 Longitude.1 i.Datetime i.Latitude i.Longitude
# 1: 2018-10-01 09:57:10 34.90141 104 2018-10-01 07:57:10 34.70141 103.8 2018-10-01 08:57:10 34.80141 103.9