I have a dataset in wide format with no time variable and I would like to create a time variable and turn it into long format for longitudinal analysis. The original dataset looks like this:
id <- c(1,2,3)
pdq1 <- c(3,5,6)
pdq2 <- c(1,3,4)
pdq3 <- c(4,5,4)
scor_abp1 <- c(1,2,3)
scor_abp2 <- c(2,2,4)
scor_abp3 <- c(1,4,5)
dat <- data.frame(id,pdq1,pdq2,pdq3,scor_abp1,scor_abp2,scor_abp3)
Desired output:
id <- c(1,1,1,2,2,2,3,3,3)
time <- c(1,2,3,1,2,3,1,2,3)
pdq <- c(3,1,4,5,3,5,6,4,4)
abp <- c(1,2,1,2,2,4,3,4,5)
dat <- data.frame(id,time,pdq,abp)
pdq and scor_abp are two different measures. The variables with suffix 1 means it is measured at Time 1, with suffix 2 means it is measured at Time 2, etc.
I would appreciate all the help there is!!! Thanks in advance!