With the data below, I want to expand the rows or each level of the IndID factor so that there are as many rows as there are years between the CptrDt and MortDt, including the starting and ending years. For individuals without a MortDt I hope to fill in the years sequential to 2017.
dat <- data.frame(IndID = c("AAA","BBB","CCC"),
CptrDt = as.Date(c("01-01-2013" ,"01-01-2013", "01-01-2014"),"%m-%d-%Y"),
MortDt = as.Date(c("01-01-2015" ,"01-01-2016", NA),"%m-%d-%Y"))
> dat
IndID CptrDt MortDt
1 AAA 2013-01-01 2015-01-01
2 BBB 2013-01-01 2016-01-01
3 CCC 2014-01-01 <NA>
The simplified result would return only the year as shown below, but I can work with other date formats.
Result <- data.frame(IndID = c(rep("AAA",3), rep("BBB",4), rep("CCC",4)),
Year = c(2013,2014,2015,2013,2014,2015,2016,2014,2015,2016,2017))
IndID Year
1 AAA 2013
2 AAA 2014
3 AAA 2015
4 BBB 2013
5 BBB 2014
6 BBB 2015
7 BBB 2016
8 CCC 2014
9 CCC 2015
10 CCC 2016
11 CCC 2017
I recognize this question is very similar to a previous post, but given the presence of NA values and slightly different data structure, I have not been able to produce the desired result with the previous response and would appreciate any suggestions. Moreover, as seen in the posted answers, there are additional solutions.