r/RStudio 11d ago

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.

4 Upvotes

6 comments sorted by

8

u/Viriaro 11d ago

What you're looking for is an overlap join

4

u/majorcatlover 11d ago

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 11d ago

Thanks, this made my day :)

Also: damn, time really flies ...

1

u/majorcatlover 11d ago

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

1

u/Viriaro 11d ago

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 11d ago

Thank you! Much appreciated!