How to Melt Data to Long Format in R - dummies

How to Melt Data to Long Format in R

By Andrie de Vries, Joris Meys

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