I have a dataset of episodes by ID, where for each person I have a start date and a length of the episode (df.have).
I'd like to create start and end dates for each episode where the start date for one episode is one day after the end date for the previous episode (df.want)
I know I need to lag the prior start date but I don't know how to do that repeatedly (i.e., I can do it for the second episode, but not the third).
df.have <- data.frame(id=c(1,1,1,2,2,2,3,3,3),
episode_num=c(1,2,3,1,2,3,1,2,3),
start_date=as.Date(c("1/1/2001", NA, NA, "5/1/2001", NA, NA, "10/1/1001", NA, NA), "%m/%d/%y"),
episode_length=c(10,4,5,20,3,2,1,9,8))
df.want <- df <- data.frame(id=c(1,1,1,2,2,2,3,3,3),
episode_num=c(1,2,3,1,2,3,1,2,3),
start_date=as.Date(c("1/1/01", "1/12/01","1/17/01","5/1/01","5/22/01","5/26/01","10/1/01","10/3/01","10/13/01"),"%m/%d/%y"),
end_date= as.Date(c("1/11/01","1/16/01","1/22/01","5/21/01","5/25/01","5/28/01","10/2/01","10/12/01","10/21/01"), "%m/%d/%y"),
episode_length=c(10,4,5,20,3,2,1,9,8))