I am trying to reshape a wide data frame into a long one. The variables are "id", "age1", "read1", "age2", "read2", "age3", "read3, "age4", "read4", age5", and "read6". Basically, the study measured six reading scores of students (read1:read6) in six age points (age1:age6). Each student has an id (1:407). What I am trying to get is a longitudinal df with three variables, "id", "age", and "reading", of which the latter two are 'measured'.
This is the wide df:
I would like to get something like this (from MLwiN)
I used the following code, which you might laugh at, but it was the best I could pull off:
dflong<- melt(df, id = "id", measure.vars = c("age1", "age2", "age3", "age4", "age5", "age6"), c("read1", "read2", "read3", "read4", "read5", "read6"))
Needless to say, I did not work. I got the classical warning:
Error in match.names(clabs, names(xi)) : names do not match previous names
Can someone please help me out? Thank you!