this is my first question here. So please be merciful with me :-). I am grateful for any suggestions for improvement.
Here is my question:
I have a grouped and sorted tibble and I want to go through row by row and make comparisons with both the previous row and the next row. For this I have to treat the first and the last row differently. Each row has an interval defined by "start" and "end" and I want to find out if these intervals overlap.
The "if" function does not do what what I want it to do. I have tried a lot of things but I can't get a result. Maybe there is a much simpler solution.
Thanks in advance for helping!
library(tidyverse)
library(lubridate)
ID <- c(1, 1, 1, 1, 2, 2, 2, 2)
start <- ymd_hms(c("2022-04-15 10:10:00", "2022-04-15 10:15:00", "2022-04-15 10:35:00", "2022-04-15 10:50:00", "2022-04-15 11:20:00", "2022-04-15 11:35:00", "2022-04-15 11:45:00", "2022-04-15 11:50:00"))
end <- ymd_hms(c("2022-04-15 10:20:00", "2022-04-15 10:30:00", "2022-04-15 10:40:00", "2022-04-15 10:55:00", "2022-04-15 11:30:00", "2022-04-15 11:40:00", "2022-04-15 11:55:00", "2022-04-15 11:55:00"))
data <- tibble(ID, start, end)
data %>%
group_by(ID) %>%
arrange(start, .by_group = TRUE) %>%
mutate(overlap = {
if (row_number()==1) {lead(start) < end
} else if (row_number()==n()) {start < lag(end)
} else {(lead(start) < end) | (start < lag(end))}
})
# A tibble: 8 x 4
# Groups: ID [2]
ID start end overlap
<dbl> <dttm> <dttm> <lgl>
1 1 2022-04-15 10:10:00 2022-04-15 10:20:00 TRUE
2 1 2022-04-15 10:15:00 2022-04-15 10:30:00 FALSE
3 1 2022-04-15 10:35:00 2022-04-15 10:40:00 FALSE
4 1 2022-04-15 10:50:00 2022-04-15 10:55:00 NA
5 2 2022-04-15 11:20:00 2022-04-15 11:30:00 FALSE
6 2 2022-04-15 11:35:00 2022-04-15 11:40:00 FALSE
7 2 2022-04-15 11:45:00 2022-04-15 11:55:00 TRUE
8 2 2022-04-15 11:50:00 2022-04-15 11:55:00 NA
My expected output would be:
# A tibble: 8 x 4
# Groups: ID [2]
ID start end overlap
<dbl> <dttm> <dttm> <lgl>
1 1 2022-04-15 10:10:00 2022-04-15 10:20:00 TRUE
2 1 2022-04-15 10:15:00 2022-04-15 10:30:00 TRUE
3 1 2022-04-15 10:35:00 2022-04-15 10:40:00 FALSE
4 1 2022-04-15 10:50:00 2022-04-15 10:55:00 FALSE
5 2 2022-04-15 11:20:00 2022-04-15 11:30:00 FALSE
6 2 2022-04-15 11:35:00 2022-04-15 11:40:00 FALSE
7 2 2022-04-15 11:45:00 2022-04-15 11:55:00 TRUE
8 2 2022-04-15 11:50:00 2022-04-15 11:55:00 TRUE