I have a longitudinal survey dataset. I want to transform from long to wide so I can determine discrepancies from the first wave to the second wave. I am struggling to transform in R, as this was relatively straightforward coming from a Stata user.
I have a ID and time column with two different response columns. I want the transformed response columns to take the value of the time column in a wide format, but struggling with the code. I get a resulting dataframe with NAs.
df <- tibble(PIN = c(1001, 1001, 1002, 1002), time = c(1, 2, 1, 2), age = c(84, 86, 22, 24), height = c(58, 58, 60, 62))
df_wide <- reshape(df,
timevar=c("time"),
idvar=c("PIN"),
dir="wide")
Input:
id | time | age | height |
---|---|---|---|
1001 | 1 | 84 | 58 |
1001 | 2 | 86 | 58 |
1002 | 1 | 22 | 60 |
1002 | 2 | 24 | 62 |
Desired Output:
id | age_t1 | age_t2 | height_t1 | height_t2 |
---|---|---|---|---|
1001 | 84 | 86 | 58 | 58 |
1002 | 22 | 24 | 60 | 62 |
Actual Output:
id | age.c(1,2) | height.c(1,2) |
---|---|---|
1001 | NA | NA |
1002 | NA | NA |