Reshaping
The goal is to get tidy data. That means to have:
Each variable forms a column
Each observation forms a row
Each table/file stores data about one kind of observations.
Data to reshape
We will use standard R data set mtcars.
head(mtcars)Install reshape2 package
Nice article about respahe2
install.packages("reshape2")
library(reshape2)Melting data set
We will pass the following variables into melt function:
data set
what columns are IDs
what columns are variables
The following code will add one more column with row names and primarilly, make IDs from "carname", "gear" and "cyl". The remaining columns will be melted down.
Let's have a look at the melted values.
The better way to see what happend is to display values in table. Watch values in variable column.
Casting data sets
We can cast the data set into different shapes.
We can specify function to aggregate values.
Average Values
We will InsectSprays data set.
The following code is using tapply function to show sum of count column for each spray.
Split values
Split and sum
Combine
We can combine the values craeted above in two ways.
Using plyr package to summarize values
Be careful, there is
summariseand NOTsummarize. If you do that mistake you get argument "by" is missing, with no default error.
More to lear on http://www.r-bloggers.com/a-fast-intro-to-plyr-for-r
Other functions
acastcasting multi-dimensional arrayarrangefaster reordering without using order() functionmutateadding new variables
Last updated
Was this helpful?