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"))ขับเคลื่อนโดย
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.
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
newdata
subjmeans
timemeans
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.