How do i left join 2 different tables based on license plate number and time?

Viewed 54

I'm new to R and trying to left join 2 sets of data based on License Plate Number and Datetime.

Data Set 1

LicensePlate DateTime
XLP1234P     09-JUN-18 02.52.40.144000000 PM
XLP2345P     18-JUL-18 11.22.46.855000000 AM
XLP3456P     18-JUL-18 11.22.46.856000000 AM
XLP4567P     18-JUL-18 11.22.46.856000000 AM
XLP5678P     18-JUL-18 11.22.46.857000000 AM
XLP6789P     18-JUL-18 11.22.46.858000000 AM

Data Set 2

LicensePlate DateTime
XLP1234P     09-JUN-18 02.55.40.144000000 PM 
XLP2345P     18-JUL-18 11.30.46.855000000 AM

Basically, the dataset is recorded by 2 different sets of equipment and hence there will be a slight time difference. I would like to left join onto the first set based on license plate at an acceptable time difference of 10minutes.

left_join allows me to merge data by column value but how do i set a condition whereby the datetime is of a suitable range?

3 Answers

Here is a possible non-equi join approach using package. I will take this down if OP is only looking for a approach

DT1[, c("start", "end") := .(DateTime - 60*10, DateTime + 60*10)]
DT2[DT1, on=.(LicensePlate=LicensePlate, DateTime>=start, DateTime<=end),
    .(LicensePlate, i.DateTime, x.DateTime)]

output:

   LicensePlate          i.DateTime          x.DateTime
1:     XLP1234P 2018-06-09 02:52:40 2018-06-09 02:55:40
2:     XLP2345P 2018-07-18 11:22:46 2018-07-18 11:30:46
3:     XLP3456P 2018-07-18 11:22:46                <NA>
4:     XLP4567P 2018-07-18 11:22:46                <NA>
5:     XLP5678P 2018-07-18 11:22:46                <NA>
6:     XLP6789P 2018-07-18 11:22:46                <NA>

data:

library(data.table)
DT1 <- fread("LicensePlate,DateTime 
XLP1234P,09-JUN-18 02.52.40.144000000 PM 
XLP2345P,18-JUL-18 11.22.46.855000000 AM 
XLP3456P,18-JUL-18 11.22.46.856000000 AM 
XLP4567P,18-JUL-18 11.22.46.856000000 AM 
XLP5678P,18-JUL-18 11.22.46.857000000 AM 
XLP6789P,18-JUL-18 11.22.46.858000000 AM")

DT2 <- fread("LicensePlate,DateTime
XLP1234P,09-JUN-18 02.55.40.144000000 PM 
XLP2345P,18-JUL-18 11.30.46.855000000 AM")

DT1[, DateTime := as.POSIXct(DateTime, format="%d-%b-%y %H.%M.%OS")]
DT2[, DateTime := as.POSIXct(DateTime, format="%d-%b-%y %H.%M.%OS")]

using the data provided by the answer from @chinsoon12

here is an other way to use data.table; a rolling join on a 10-minute window

#set keys
setkey( DT1, LicensePlate, DateTime )
setkey( DT2, LicensePlate, DateTime )
#rolling update join, looking 10 minutes ahead
DT1[, DateTime2 := DT2[DT1, x.DateTime, roll = -600 ] ][]

#    LicensePlate            DateTime           DateTime2
# 1:     XLP1234P 2018-06-09 02:52:40 2018-06-09 02:55:40
# 2:     XLP2345P 2018-07-18 11:22:46 2018-07-18 11:30:46
# 3:     XLP3456P 2018-07-18 11:22:46                <NA>
# 4:     XLP4567P 2018-07-18 11:22:46                <NA>
# 5:     XLP5678P 2018-07-18 11:22:46                <NA>
# 6:     XLP6789P 2018-07-18 11:22:46                <NA>

The easiest way to do this would be to create a joining variable that is rounded to the range that you choose.

dataset_2 <- dataset_2 %>%
    mutate(join_date = lubridate::round_date(DateTime, "10 minutes"))

dataset_1 <- dataset_1 %>%
    mutate(join_date = lubridate::round_date(DateTime, "10 minutes")) %>%
    left_join(dataset_2, by = c("LicensePlate", "join_date"))
Related