I am trying to perform a left join on two dataframes based on nearest timestamp. The sample data looks like this:
> df1
ID date1
1 1 2020-07-11 19:14:23
2 1 2020-07-21 13:11:10
3 1 2020-07-21 18:07:25
4 1 2020-07-28 18:18:11
5 2 2020-07-13 16:47:26
6 2 2020-07-18 17:11:37
7 3 2020-07-23 10:39:19
> df2
ID date2 Flag
1 1 2020-07-11 18:14:23 Yes
2 1 2020-07-20 14:21:11 Yes
3 2 2020-07-13 17:18:13 Yes
4 2 2020-07-18 15:12:06 Yes
I want to merge the two data frames by ID and dates columns so that the Flag column can be joined in df1 to the nearest date. Result is to look like this
> Combined
ID date1 Flag
1 1 2020-07-11 19:14:23 Yes
2 1 2020-07-21 13:11:10 Yes
3 1 2020-07-21 18:07:25
4 1 2020-07-28 18:18:11
5 2 2020-07-13 16:47:26 Yes
6 2 2020-07-18 17:11:37 Yes
7 3 2020-07-23 10:39:19
Couldn't find an apt solution. Please help.