HOW CAN I RESHAPE MY DATA IN R? | R FAQ
When there are multiple measurements of the same subject, across time or using different tools, the data is often described as being in "wide" format if there is one observation row per subject with each measurement present as a different variable and "long" format if there is one observation row per measurement (thus, multiple rows per subject). Different functions require different formats, and so the need to reshape a dataset may arise.
Below, we start with a dataset in wide format. Students have been measured using five metrics: read, write, math, science, and socst.
hsb2 <- read.table('https://stats.idre.ucla.edu/stat/r/faq/hsb2.csv', header=T, sep=",")
hsb2[1:10,]id female race ses schtyp prog read write math science socst1 70 0 4 1 1 1 57 52 41 47 572 121 1 4 2 1 3 68 59 53 63 613 86 0 4 3 1 1 44 33 54 58 314 141 0 4 3 1 3 63 44 47 53 565 172 0 4 2 1 2 47 52 57 53 616 113 0 4 2 1 2 44 52 51 63 617 50 0 3 2 1 1 50 59 42 53 618 11 0 1 2 1 2 34 46 45 39 369 84 0 4 2 1 1 63 57 54 58 5110 48 0 3 2 1 2 57 55 52 50 51
To reformat this dataset into long form, we will use the reshape function. The arguments we provide include a list of variable names that define the different times or metrics (varying), the name we wish to give the variable containing these values in our long dataset (v.names), the name we wish to give the variable describing the different times or metrics (timevar), the values this variable will have (times), and the end format for the data (direction). Additionally, we have provided new row names.
## Wide to long
l <- reshape(hsb2,
varying = c("read", "write", "math", "science", "socst"),
v.names = "score",
timevar = "subj",
times = c("read", "write", "math", "science", "socst"),
new.row.names = 1:1000,
direction = "long")
l.sort <- l[order(l$id),]
l.sort[1:10,]id female race ses schtyp prog subj score99 1 1 1 1 1 3 read 34299 1 1 1 1 1 3 write 44499 1 1 1 1 1 3 math 40699 1 1 1 1 1 3 science 39899 1 1 1 1 1 3 socst 41139 2 1 1 2 1 3 read 39339 2 1 1 2 1 3 write 41539 2 1 1 2 1 3 math 33739 2 1 1 2 1 3 science 42939 2 1 1 2 1 3 socst 41
After sorting by id, we can see that we have five rows per student and their five scores appear in the score variable with the subj variable indicating which test on which the score was measured. For each of the five rows per id, the female, race, ses, schtyp, and prog variables are unchanging.
We can similarly go from this long form back to our original wide form again using reshape with different arguments (most importantly, direction = "wide"). With timevar, we indicate the variable that will define the multiple measurements per subject. With idvar, we list the variables that should do not vary within subject.
## Long to wide
w <- reshape(l.sort,
timevar = "subj",
idvar = c("id", "female", "race", "ses", "schtyp", "prog"),
direction = "wide")
w[1:10,]id female race ses schtyp prog score.read score.write score.math score.science score.socst99 1 1 1 1 1 3 34 44 40 39 41139 2 1 1 2 1 3 39 41 33 42 4184 3 0 1 1 1 2 63 65 48 63 56112 4 1 1 1 1 2 44 50 41 39 5176 5 0 1 1 1 2 47 40 43 45 31149 6 1 1 1 1 2 47 41 46 40 4150 7 0 1 2 1 2 57 54 59 47 5194 8 1 1 1 1 2 39 44 52 44 4860 9 0 1 2 1 3 48 49 52 44 51154 10 1 1 2 1 1 47 54 49 53 61
Once again, we have one observation per student. We can compare the dimensions of our wide dataset w to our original dataset:
dim(w)[1] 200 11dim(hsb2)[1] 200 11