I have a data set that includes a range of dates and need to fill in the missing dates in new rows. df1 is an example of the data I am working with and df2 is an example of what I've managed to achieve (where I'm stuck). df3 is where I would like to end up!
df1
ID Date DateStart DateEnd
1 2/11/2021 2/11/2021 2/17/2021
1 2/19/2021 2/19/2021 2/21/2021
2 1/15/2021 1/15/2021 1/20/2021
2 1/22/2021 1/22/2021 1/23/2021
This is where I am with this. The NAs aren't an issue because I intend to drop the DateStart and DateEnd columns after doing what I need to do. The issue here is that I don't want to include the dates that fall within the previous DateStart and DateEnd range.
To get here I grouped by ID and filled in the missing dates between the dates in df1:
df2
ID Date DateStart DateEnd
1 2/11/2021 2/11/2021 2/17/2021
1 2/12/2021 NA NA
1 2/13/2021 NA NA
1 2/14/2021 NA NA
1 2/15/2021 NA NA
1 2/16/2021 NA NA
1 2/17/2021 NA NA
1 2/18/2021 NA NA
1 2/19/2021 2/19/2021 2/21/2021
2 1/15/2021 1/15/2021 1/20/2021
2 1/16/2021 NA NA
2 1/17/2021 NA NA
2 1/18/2021 NA NA
2 1/19/2021 NA NA
2 1/20/2021 NA NA
2 1/21/2021 NA NA
2 1/22/2021 NA NA
2 1/23/2021 1/23/2021 1/24/2021
This is actually what I'd like to end up with:
df3
ID Date DateStart DateEnd
1 2/11/2021 2/11/2021 2/17/2021
1 2/18/2021 NA NA
1 2/19/2021 2/19/2021 2/21/2021
2 1/15/2021 1/15/2021 1/20/2021
2 1/21/2021 NA NA
2 1/22/2021 NA NA
2 1/23/2021 1/23/2021 1/24/2021
In df3 the missing dates are filled in but not the dates within the DateStart-DateEnd range.
Any thoughts on how to achieve this? Note: I have a dataset with a large number of observations.