Right now, my dataset is in wide format, meaning I have one row per person, but I want a long dataset, with multiple rows per person. I have two date variables, ADATE and DDATE, that I want to use as my start and end points, respectively. For example, if someone's ADATE is 02/04/10 and DDATE is 02/07/10, I want 4 rows:
Have:
ID ADATE DDATE
1 02/04/10 02/07/10
Want:
ID ADATE DDATE NEW_DATE
1 02/04/10 02/07/10 02/04/10
1 02/04/10 02/07/10 02/05/10
1 02/04/10 02/07/10 02/06/10
1 02/04/10 02/07/10 02/07/10
I have multiple datasets that I want to do this for, and I have written code that works for every single dataset except one...I'm not sure why. This is my attempt and the error I get:
jan15_long <- chf_jan15 %>%
mutate(NEW_DATE = as.Date(ADATE)) %>%
group_by(ID) %>%
complete(NEW_DATE = seq.Date(as.Date(ADATE), as.Date(DDATE), by = "day")) %>%
fill(vars) %>%
ungroup()
Error in seq.Date(as.Date(ADATE), as.Date(DDATE), by = "day") :
'from' must be of length 1
The above code gives me what I want and runs perfectly for every other dataset I have (10 out of 11).
Is there a better way to do this? dplyr makes the most sense to me, so hopefully there's a solution to this.