0

I have a data.frame (df1) with start and end dates and I refer to the time between start and end dates as windows. Across the rows in df1, some of the windows overlap. For example, the end date in row 1 is the same as the start date in row 3. I would like to "merge" these rows into episodes -- periods of consecutive coverage. The resulting data.frame should look like df2.

df1 <- data.frame(ID = c(1, 1, 1, 1, 1, 1),
              startdate = c('2012-02-21','2012-03-01','2012-02-23','2012-02-17','2012-02-18','2014-03-17'),
              enddate = c('2012-02-23','2012-03-06','2012-02-28','2012-02-18','2012-02-21','2014-03-22'))


df2 <- data.frame(ID = c(1, 1, 1),
              startdate = c('2012-02-17','2012-03-01','2014-03-17'),
              enddate = c('2012-02-28','2012-03-06','2014-03-22'))
user1389960
  • 323
  • 3
  • 10

0 Answers0