I have a dataframe that I'd like to have a column label collapse record based on 2 condition:
Overlapping: the second event start date is either the same with first event start date or between first event start date and end date
Contiguous: the second event start date is either the same with first event end date or within 1 day difference.
I'd already group_by (ID, AE) and lag start date and end date, but not sure how to deal with first event record "NA", which bring me trouble for the next step:enter image description here
I have already sorted start date within group and add overlapping/ contagious
I am expecting to get result like this:
ADD column to label collapse event record and within collapse record chose the max grade and earliest start date and end date: enter image description here
Part of data look like this:
DF = data.frame(Id=rep(s001, 4), AE = rep(dermatitis, 4), start_date= c("06/18/204", "11/20/2014", 12/03/2014", 03/21/2015"), end_date= c("10/21/2014", "12/03/1024", 03/20/2015", 04/22/2015"), grade = c(2, 1,2,1))