0

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:

Wide dataframe

I would like to get something like this (from MLwiN)

Long dataframe 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!

  • Please do not post data as images because we cannot copy/paste those values into R. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 23 '21 at 15:37

0 Answers0