Hi I have two tables (table1 and table2 below) and would like to join them based on the closest timestamp to form expected_output. Some kind of solution involving dplyr wou
Using rolling joins feature of data.table
with roll = "nearest"
:
require(data.table) # v1.9.6+
setDT(table1)[, val2 := setDT(table2)[table1, val2, on = "date", roll = "nearest"]]
Here, val2
column is created by performing a join on the column date
with roll = "nearest"
option. For each row of table1$date
, the closest matching row from table2$date
is computed, and val2
for corresponding row is extracted.