In future, please post your data in a copy & paste-friendly format by following the guide here. It's tedious to recreate data from the format you've shared so I'm only going to recreate a few records.
I'm making a couple of assumptions regarding your question. There is no variable in your dataset called AESTDT
so I presume you mean AESTDTC
. Secondly, since you've said that the last two records have overlapping intervals, I presume that entries having the same AESTDTC
and AEENDTC
are also considered as overlapping.
Your puzzle is challenging and the solution I've provided is somewhat ugly but does the job.
library(tidyverse)
library(lubridate, warn.conflicts = FALSE)
df <- tribble(
~PATIENT, ~AESTDTC, ~AEENDTC, ~AETERM1, ~AEPT,
1234, "1-Sep-19", "29-Sep-19", "THROMBOCYTOPENIA", "THROMBOCYTOPENIA",
1234, "20-Sep-19", "1-Oct-19", "THROMBOCYTOPENIA", "THROMBOCYTOPENIA",
1234, "1-Dec-19", "3-Dec-19", "THROMBOCYTOPENIA", "THROMBOCYTOPENIA",
1234, "4-Jan-18", "31-Jan-18", "NEUTROPENIA", "NEUTROPENIA",
1234, "3-Jul-19", "30-Jul-19", "NEUTROPENIA", "NEUTROPENIA",
3134, "16-Jan-20", NA, "LEUKOPENIA", "LEUKOPENIA",
3134, "4-Sep-12", "7-Sep-12", "LEUKOPENIA", "LEUKOPENIA",
3134, "21-Aug-12", "4-Sep-12", "LEUKOPENIA", "LEUKOPENIA"
)
df <- mutate_at(df, vars(AESTDTC, AEENDTC), dmy)
intervals <- df %>%
mutate(AE_interval = interval(AESTDTC, AEENDTC)) %>%
group_by(PATIENT, AEPT) %>%
summarise(AE_interval = list(AE_interval), .groups = "drop")
records_with_overlap <- df %>%
left_join(intervals, by = c("PATIENT", "AEPT")) %>%
mutate(overlap = map2(AESTDTC, AE_interval, `%within%`)) %>%
rowwise() %>%
mutate(overlap_count = reduce(overlap, sum)) %>%
ungroup() %>%
filter(overlap_count > 1)
semi_join(df, records_with_overlap)
#> Joining, by = c("PATIENT", "AESTDTC", "AEENDTC", "AETERM1", "AEPT")
#> # A tibble: 2 x 5
#> PATIENT AESTDTC AEENDTC AETERM1 AEPT
#> <dbl> <date> <date> <chr> <chr>
#> 1 1234 2019-09-20 2019-10-01 THROMBOCYTOPENIA THROMBOCYTOPENIA
#> 2 3134 2012-09-04 2012-09-07 LEUKOPENIA LEUKOPENIA
Created on 2020-09-04 by the reprex package (v0.3.0)