Find Overlapping Intervals When Data Frames are Different Lengths

The join_by() function of dplyr with its within() and overlaps() helpers may be what you need.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

NO <- read.csv("~/R/Play/NO.csv")
CO <- read.csv("~/R/Play/CO.csv")
PM <- read.csv("~/R/Play/PM.csv")

NO <- NO |> mutate(across(2:3, ymd_hm))
CO <- CO |> mutate(across(2:3, ymd_hm))
PM <- PM |> mutate(across(2:3, ymd_hm))
#> Warning: There was 1 warning in `mutate()`.
#> ℹ In argument: `across(2:3, ymd_hm)`.
#> Caused by warning:
#> !  2 failed to parse.

#which NO envents started and ended during a CO event?
NO_CO <- inner_join(NO, CO, join_by(within(NO_Start, NO_End, CO_Start, CO_End)))
head(NO_CO)
#>   Event.x            NO_Start              NO_End Event.y            CO_Start
#> 1       9 2023-07-22 07:00:00 2023-07-22 13:00:00      12 2023-07-21 06:00:00
#> 2      12 2023-08-06 05:00:00 2023-08-06 13:00:00      19 2023-08-05 09:00:00
#> 3      15 2023-08-12 21:00:00 2023-08-13 02:00:00      23 2023-08-12 12:00:00
#> 4      20 2023-08-28 03:00:00 2023-08-28 13:00:00      33 2023-08-27 17:00:00
#> 5      23 2023-09-07 09:00:00 2023-09-07 14:00:00      37 2023-09-06 15:00:00
#> 6      24 2023-09-07 20:00:00 2023-09-08 02:00:00      37 2023-09-06 15:00:00
#>                CO_End
#> 1 2023-07-22 23:00:00
#> 2 2023-08-07 02:00:00
#> 3 2023-08-13 10:00:00
#> 4 2023-08-28 13:00:00
#> 5 2023-09-08 11:00:00
#> 6 2023-09-08 11:00:00

#which NO events overlap CO events
NO_CO_2 <- inner_join(NO, CO, join_by(overlaps(NO_Start, NO_End, CO_Start, CO_End)))
head(NO_CO_2)
#>   Event.x            NO_Start              NO_End Event.y            CO_Start
#> 1       5 2023-07-09 02:00:00 2023-07-09 13:00:00       8 2023-07-08 07:00:00
#> 2       9 2023-07-22 07:00:00 2023-07-22 13:00:00      12 2023-07-21 06:00:00
#> 3      10 2023-07-24 06:00:00 2023-07-24 13:00:00      13 2023-07-23 03:00:00
#> 4      12 2023-08-06 05:00:00 2023-08-06 13:00:00      19 2023-08-05 09:00:00
#> 5      13 2023-08-07 02:00:00 2023-08-07 11:00:00      19 2023-08-05 09:00:00
#> 6      15 2023-08-12 21:00:00 2023-08-13 02:00:00      23 2023-08-12 12:00:00
#>                CO_End
#> 1 2023-07-09 10:00:00
#> 2 2023-07-22 23:00:00
#> 3 2023-07-24 06:00:00
#> 4 2023-08-07 02:00:00
#> 5 2023-08-07 02:00:00
#> 6 2023-08-13 10:00:00

Created on 2024-02-22 with reprex v2.0.2

1 Like