HAVE = fread("
STUDENT CLASS GROUP S1TIME S2TIME S3TIME H2TAB H4TAB H5TAB
1 0 2 NA NA 6 NA 7 5
2 0 7 4 NA 4 5 NA 2
3 1 5 NA 4 5 6 NA NA
")
WANT = data.frame(
STUDENT = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
INPUT = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
CLASS = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1),
GROUP = c(2, 2, 2, 2, 2, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5),
S.TIME = c(NA, NA, 6, NA, NA, 4, NA, 4, NA, NA, NA, 4, 5, NA, NA),
H.TAB = c(NA, NA, NA, 7, 5, NA, 5, NA, NA, 2, NA, 6, NA, NA, NA)
)
Is it possible to transform HAVE into WANT without specifying "TIME, TAB" variables? I wish to extract time-varying stubs and reshape full data
Here is a solution to pivot longer, clean up the columns and then pivot_wider into the desired form.
I am sure there is a way to perform this in one line, but this was easier to debug.