I've seen various solutions for this question based on date only, but the time component is tripping me up. I have two data frames with POSIXct columns called 'datetime'. For DF1 that column has data rounded to the nearest hour. For DF2, the time component is not rounded to the nearest hour and can occur anytime. The dataframes look like this:
DF1
datetime | X | Y | Z |
---|---|---|---|
2020-09-01 03:00:00 | 1 | 3 | 4 |
2020-09-02 12:00:00 | 12 | 3 | 5 |
2020-09-02 22:00:00 | 4 | 9 | 19 |
2020-09-03 01:00:00 | 4 | 10 | 2 |
2020-09-04 06:00:00 | 4 | 12 | 1 |
2020-09-04 08:00:00 | 11 | 13 | 10 |
DF2
datetime | Var |
---|---|
2020-09-01 02:23:14 | A |
2020-09-01 03:12:09 | B |
2020-09-02 11:52:15 | A |
2020-09-02 12:15:44 | B |
2020-09-02 22:31:56 | A |
2020-09-02 21:38:05 | B |
2020-09-03 01:11:39 | A |
2020-09-03 00:59:33 | B |
2020-09-04 05:12:19 | A |
2020-09-04 06:07:09 | B |
2020-09-04 08:22:28 | A |
2020-09-04 07:50:17 | B |
What I want is to merge these two dataframes based on this column using the date and time that are closest in time to 'datetime' in DF1, so that it looks like this:
datetime | X | Y | Z | Var |
---|---|---|---|---|
2020-09-01 03:00:00 | 1 | 3 | 4 | B |
2020-09-02 12:00:00 | 12 | 3 | 5 | A |
2020-09-02 22:00:00 | 4 | 9 | 19 | B |
2020-09-03 01:00:00 | 4 | 10 | 2 | B |
2020-09-04 06:00:00 | 4 | 12 | 1 | B |
2020-09-04 08:00:00 | 11 | 13 | 10 | B |
Thank you!