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.

About This Article

This article is from the book:

About the book authors:

Andrie de Vries is a leading R expert and Business Services Director for Revolution Analytics. With over 20 years of experience, he provides consulting and training services in the use of R. Joris Meys is a statistician, R programmer and R lecturer with the faculty of Bio-Engineering at the University of Ghent.

This article can be found in the category: