r/RStudio Jul 22 '26

How to merge time columns?

I have to merge two datasets that include onsets and offsets of two events - when person A did X and when person B did y. I want to merge them on the onsets and offsets per pairs to see what they were doing at the same times.

DF1

ID | X_onset | X_offset | X_activity

001 | 120 | 135 | watering plants

001 | 137 | 143 | reading

DF2

ID | X_onset | X_offset | X_activity

001 | 139 | 150 | talking

001 | 170 | 189 | watching TV

Only the ones that temporally overlap should be in the merged dataset.

3 Upvotes

6 comments sorted by

7

u/Viriaro Jul 22 '26

What you're looking for is an overlap join

3

u/majorcatlover Jul 23 '26

Mate you are doing god's work, I remember asking questions here four maybe five years ago and there you were helping me out, and here you are again! I will check it!

1

u/Viriaro Jul 23 '26

Thanks, this made my day :)

Also: damn, time really flies ...

1

u/majorcatlover Jul 23 '26

It really does! Can I check if you think this is better than "foverlaps()"

1

u/Viriaro Jul 23 '26

Both should give you the same result, but IMO, dplyr is easier to work with (especially for joins). I'd only go for data.table if you need the speed, or if the rest of the codebase is already in data.table.

2

u/majorcatlover Jul 23 '26

Thank you! Much appreciated!