I need to reshape a df, complete it with missing years and create a variable tracking status change. The issue is that some values are missing and the code I wrote brakes down on those.
Toy example:
library(data.table)
df <- data.frame(id=c(1,2),phase_1=c(1994,1994),phase_2=c(1996,1996),phase_3=c(1997,NA))
df1 = melt(df,
id.vars = "id",
measure.vars = c("phase_1", "phase_2", "phase_3"),
variable.name = "status",
value.name = "year",
na.rm = FALSE)
df2 <- df1 %>% complete(id, year = full_seq(year, 1)) %>%
fill(status)
Desired
id year phase change
1 1 1994 phase_1 0
2 1 1995 phase_1 0
3 1 1996 phase_2 1
4 1 1997 phase_3 1
5 2 1994 phase_1 0
6 2 1995 phase_1 0
7 2 1996 phase_2 1
8 2 1997 phase_2 0