In R, I have data where each person has multiple session dates, and the scores on some tests, but this is all in one row. I would like to change it so I have multiple rows with the persons info, but only one of the session dates and corresponding test scores, and do this for every person. Also, each person may have completed different number of sessions.
Ex:
ID Name Session1Date Score Score Session2Date Score Score
23 sjfd 20150904 2 3 20150908 5 7
28 addf 20150905 3 4 20150910 6 8
To:
ID Name SessionDate Score Score
23 sjfd 20150904 2 3
23 sjfd 20150908 5 7
28 addf 20150905 3 4
28 addf 20150910 6 8
You can use
melt
from the devel version ofdata.table
ie.v1.9.5
. It can take multiple 'measure' columns as a list. Instructions to install arehere
Or using
reshape
frombase R
, we can specify thedirection
as 'long' andvarying
as alist
of column indexIf needed, the
rownames
can be changedUpdate
If the columns follow a specific order i.e. 3rd grouped with 6th, 4th with 7th, 5th with 8th, we can create a
matrix
of column index and thensplit
to get thelist
for thevarying
argument inreshape
.