I am trying to restructure my data using reshape
from wide to long format but I keep getting an error. Below I wrote the code I've already tried and the error message I get.
current data structure
patientid Adh_catv1 Adh_catv2 Adh_catv3 Adh_threeitemsv1 Adh_threeitemsv2 Adh_threeitemsv3
70FD optimal optimal optimal 86 90 100
70LJ suboptimal suboptimal optimal 40 50 70
70ML optimal suboptimal suboptimal 89 55 50
desired structure
patientid Visits Adherence Adherence_threeitem
70FD visit1 optimal 86
70FD visit2 optimal 90
70FD visit3 optimal 100
70LJ visit1 suboptimal 40
70LJ visit2 suboptimal 50
70LJ visit3 optimal 70
70ML visit1 optimal 89
70ML visit2 suboptimal 55
70ML visit3 suboptimal 50
This is what I've tried so far
reshape(df, direction = 'long',
varying = c ('adh_catv1:Adh_threeitemsv3'),
timevar = 'Visits',
times = c ("visit1","visit2","visit3"),
v.names = c ('adherence','adherence_threeitem),
idvar = 'patientid')
Error in reshape(df, direction = "long", varying = c("adh_catv1:Adh_threeitemsv3"), :
length of 'varying' must be the product of length of 'v.names' and length of 'times'
Please advise on what I am doing wrong in the code above or suggest an alternative easier option using other functions.