# Reading: Reshaping Data with the reshape Package

Toggle Code Viewing: ON

As part of the course Stat 585X I am taking this semester, I will be posting a series of responses to assigned course readings. Mostly these will be my rambling thoughts as I skim papers.

The paper we are reading this week is “Reshaping Data with the reshape Package” by Hadley Wickham, published in the Journal of Statistical Software, November 2007, Volume 21, Issue 12. As you may have guessed, the paper gives a good overview of what reshaping data is, why we would want to do it, and finally how we can reshape data easily.

### What is reshaping data?

When we get data from almost any source it is rare to find it in a usable shape. Usually, data will be stored in a form that is either easy to collect, or easy to store. Most of the time, neither of these are very useful to us. Typically we will want to go from a “wide” format to a “tall” format (see diagram below), especially if we want to use ggplot2 to make interesting plots.

In a wide format, there may be multiple metrics associated with one identity, one row for each identity. In a tall format, there will be one row for each identity-metric combination and only one column for all the the metric values. The transformation between these formats is what is called reshaping data in the paper. Tall data is called “molten” in this paper and gives insight to the naming of functions in the reshape package.

### Why do it?

Because What Would Hadley Do (WWHD)? But really, to quote the man himself, “Data often has multiple levels of grouping (nested treatments, split plot designs, or repeated measurements) and typically requires investigation at multiple levels.” We should reshape because it makes life a little easier and who doesn’t need that?

### How can we reshape?

By using the reshape package, this paper highlights two useful functions: melt and cast. For a quick example of how to reshape, the paper uses the smiths dataset available in reshape. Let’s take a look.

Wide to Tall

We can see here, there is a combination identity of subject and time with three metrics: age, weight, and height. By using reshape, we can easily transform this data into a molten or tall format.

Now we have a tall dataset with one column for the name of metrics and one for the values of these metrics.

Tall to Wide Having created our molten data frame, we may want to be able to get back the wide formula. This inverse functionality is also available in the reshape package.

There are many options when it comes to specifying id variables and formulas in the melt and cast functions, but I will refer you to the paper for more details.