How to Use the apply() Function to Summarize Arrays in R
How to Use Mathematical Functions in R
How to Add Titles and Axis Labels to a Plot in R

How to Melt Data to Long Format in R

When using R, consider the words wide and long as visual metaphors for the shape of your data. In other words, wide data tends to have more columns and fewer rows compared to long data. The reshape package extends this metaphor by using the terminology of melt and cast:

  • To convert wide data to long, you melt it with the melt() function.

  • To convert long data to wide, you cast it with the dcast() function for data frames or the acast() function for arrays.

Try converting your wide data frame goals to a long data frame using melt():

> mgoals <- melt(goals)
Using Game, Venue as id variables

The melt() function tries to guess your identifier variables (if you don’t provide them explicitly) and tells you which ones it used. By default, it considers all categorical variables (that is, factors) as identifier variables. This is often a good guess, and it’s perfect for this example.

Specifying your identifier variables explicitly is a good idea. You do this by adding an argument id.vars, where you specify the column names of the identifiers:

> mgoals <- melt(goals, id.vars=c("Game", "Venue"))

The new object, mgoals, now contains your data in long format:

> mgoals
  Game Venue variable value
1  1st Bruges  Granny  12
2  2nd Ghent  Granny   4
3  3rd Ghent  Granny   5
...
10  2nd Ghent Gertrude   5
11  3rd Ghent Gertrude   6
12  4th Bruges Gertrude   7
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Format and Perform Operations on Dates and Times in R
How to Identify and Correct Dropped Dimensions in R
How to Tell R which Function to Debug
How to Track Data Correlations in R
How to Generate Your Own Error Messages in R
Advertisement

Inside Dummies.com