I have a dataset containing columns for the ID of an individual (enrolid), a start number (start), an end number (end). I want to create a person-period level dataset of all continuous periods, where a continuous period defined as instances when the start number for a row is less than or equal to the end number for the previous row + 1. For instance, for enrolid 1, the first continuous period is from 0 to 15 because the second row for this individual starts at 11 which is the first number of the end number for the last row.
This is the data I have:
have <- tibble(
enrolid = c(1,1,1,1,2,2,2,2),
start = c(0,11,19,24,2,14,17,37),
end = c(10,15,25,29,13,16,35,49)
)
This is the data I want:
want <- tibble(
enrolid = c(1,1,2,2),
continuous_cov_start = c(0,19,2,37),
continuous_cov_end = c(15,29,35,49),
continuous_cov_sequence = c(1,2,1,2)
)
Thank you! While I used numbers for start and end, I will be adapting this code to use dates. I am trying to generate a dataframe of all the unique periods of continuous insurance coverage.
I was unable to come up with a loop, mutate, or conditional function that would achieve this task.