Reshaping Data

R provides a variety of methods for reshaping data prior to analysis.

Transpose

Use the t() function to transpose a matrix or a data frame. In the later case, rownames become variable (column) names.

# example using built-in dataset
mtcars
t(mtcars)

The Reshape Package

Hadley Wickham has created a comprehensive package called reshape to massage data. Both an introduction and article are available. There is even a video!

Basically, you "melt" data so that each row is a unique id-variable combination. Then you "cast" the melted data into any shape you would like. Here is a very simple example.

mydata

id time x1 x2
1 1 5 6
1 2 3 5
2 1 6 1
2 2 2 4

 

# example of melt function
library(reshape)
mdata <- melt(mydata, id=c("id","time"))

newdata

id time variable value
1 1 x1 5
1 2 x1 3
2 1 x1 6
2 2 x1 2
1 1 x2 6
1 2 x2 5
2 1 x2 1
2 2 x2 4
# cast the melted data
# cast(data, formula, function)
subjmeans <- cast(mdata, id~variable, mean)
timemeans <- cast(mdata, time~variable, mean)

subjmeans

id x1 x2
1 4 5.5
2 4 2.5

timemeans

time x1 x2
1 5.5 3.5
2 2.5 4.5

There is much more that you can do with the melt( ) and cast( ) functions. See the documentation for more details.

Going Further

To practice massaging data, try this course in cleaning data in R.