I need to convert my dataset from wide to long format. I have used the reshape function pretty frequently and this is my first problem with it. I think it's because one variable has a value of 0 for all of time 1. This is the main part of my code:
c1dat0_long <- reshape(widedata,
varying = c("ATT0", "ATT1", "ATT2", "ATT3", "ATT4",
"ATP0", "ATP1", "ATP2", "ATP3", "ATP4",
"n_ATT0", "n_ATT1", "n_ATT2", "n_ATT3", "n_ATT4",
"cm_ATP0", "cm_ATP1", "cm_ATP2", "cm_ATP3", "cm_ATP4",
"sd_ATP0", "sd_ATP1", "sd_ATP2", "sd_ATP3", "cm_ATP4",
"n_NEW0", "n_NEW1", "n_NEW2", "n_NEW3", "n_NEW4",
"cm_AGE0", "cm_AGE1", "cm_AGE2", "cm_AGE3", "cm_AGE4",
"p_FEMALE0", "p_FEMALE1", "p_FEMALE2", "p_FEMALE3", "p_FEMALE4",
"n_FEMALE0", "n_FEMALE1", "n_FEMALE2", "n_FEMALE3", "n_FEMALE4"),
idvar = "ID", sep="", timevar = "TIME", direction = "long")
I think the problem is that the value of n_NEW0 is 0 for all subjects. It needs to be because everyone is new at time 1, so it has to be the same number. If I just used the number of people in the group then it would look like each group had a different number of "new" participants at time 1.
This is what I got:
Error in reshapeLong(data, idvar = idvar, timevar = timevar, varying = varying, :
'varying' arguments must be the same length
Is there anything I can do to make this work? I've tried using tidyr::pivot_longer instead but it hasn't worked and I can't seem to figure out where I'm going wrong there either.
PS. I just tried changing the values of n_NEW0 to vary and I'm getting the same error message so maybe I was wrong about the root of the problem.