How to Download the reshape2 Package in R

By Andrie de Vries, Joris Meys

Base R has a function, reshape(), that works fine for data reshaping. However, the original author of this function had in mind a specific use case for reshaping: so-called longitudinal data.

Longitudinal research takes repeated observations of a research subject over a period of time. For this reason, longitudinal data typically has the variables associated with time.

The problem of data reshaping is far more generic than simply dealing with longitudinal data. For this reason, Hadley Wickham wrote and released the package reshape2 that contains several functions to convert data between long and wide format.

To download and install reshape2, use install.packages():

> install.packages(“reshape2”)

At the start of each new R session that uses reshape2, you need to load the package into memory using library():

> library(“reshape2”)

Now you can start. First, create some data:

> goals <- data.frame(
+  Game = c(“1st”, “2nd”, “3rd”, “4th”),
+  Venue = c(“Bruges”, “Ghent”, “Ghent”, “Bruges”),
+  Granny = c(12, 4, 5, 6),
+  Geraldine = c(5, 4, 2, 4),
+  Gertrude = c(11, 5, 6, 7)
+ )

This constructs a wide data frame with five columns and four rows with the scores of Granny, Geraldine, and Gertrude.