I have some date/time data that has overlapping minutes/hours and I want to calculate how much there is. I'm a little stumped on how to do this. I prefer a tidyverse solution because I'm more familiar with reading it, but anything that works will be helpful. Example code / expected outcome below:
library(tidyverse)
library(lubridate)
main_start <- c("2024-01-01 4:50:00 PM", "2024-03-22 11:00:00 AM")
main_end <- c("2024-01-01 11:40:00 PM", "2024-03-22 9:00:00 PM")
second_start <- c("2024-01-01 2:00:00 PM", "2024-03-22 12:00:00 PM")
second_end <- c("2024-01-02 12:15:00 AM", "2024-03-22 8:00:00 PM")
third_start <- c("2024-01-01 8:00:00 AM", "2024-03-22 8:00:00 AM")
third_end <- c("2024-01-01 5:00:00 PM", "2024-03-22 5:00:00 PM")
df <- tibble::tibble(main_start, main_end,
second_start, second_end,
third_start, third_end) %>%
mutate(main_start = ymd_hms(main_start),
main_end = ymd_hms(main_end),
second_start = ymd_hms(second_start),
second_end = ymd_hms(second_end),
third_start = ymd_hms(third_start),
third_end = ymd_hms(third_end))
I want to find the total amount of time that MAIN time had overlapped with SECOND or THIRD
If there is a double overlap it only needs to count once
For example, in row 1
- Main time is from 4:50 PM to 11:40 PM.
- Second is from 2:00 PM to 12:15 AM the next morning.
- Third is from 8:00 AM to 5:00 PM
The entire main time is overlapping at some point with either of the other two. There is some double overlap, but I don't need that So the expected output would be 6 hours 50 minutes, or it can be in decimal form - doesn't really matter
Row 2
- Main time 11:00 AM to 9:00 PM. Second 12:00 PM to 8:00 PM. Third 8:00 AM to 5:00 PM
- The overlap here is from 11:00 AM to 8:00 PM, so it the result should be a flat 9 hours.
The only way I can think to calculate it is by listing every single minute between each option and finding the duplicates