Reshape Data Frame from Long to Wide Format in R (2 Examples)


In this R tutorial you’ll learn how to convert data frames from long to wide format.

The article is structured as follows:

Let’s dive right in!


Introduction of Example Data

Consider the following example data:

data <- data.frame(group_1 = rep(c("A", "B", "C"), each = 3),  # Create example data
                   group_2 = LETTERS[4:6],
                   values = 1:9)
data                                                           # Print example data
#   group_1 group_2 values
# 1       A       D      1
# 2       A       E      2
# 3       A       F      3
# 4       B       D      4
# 5       B       E      5
# 6       B       F      6
# 7       C       D      7
# 8       C       E      8
# 9       C       F      9

The previous output of the RStudio console shows that our example data has nine rows and three columns.

The variables group_1 and group_2 are our ID columns that define the groups of our data. The variable values contains a numeric range from 1 to 9.


Example 1: Reshaping Data Frame from Long to Wide Format Using reshape Function

In Example 1, I’ll show how to convert a data frame from long to wide format using the reshape function.

Within the reshape function, we have to specify the name of our data frame (i.e. data), the idvar argument (i.e. group_1), the timevar argument (i.e. group_2), and the direction (i.e. “wide”):

data_reshape1 <- reshape(data,                                 # Applying reshape function
                         idvar = "group_1",
                         timevar = "group_2",
                         direction = "wide")
data_reshape1                                                  # Print wide data frame
# group_1 values.D values.E values.F
# 1       A        1        2        3
# 4       B        4        5        6
# 7       C        7        8        9

The previous output shows the final result of the reshape function: A data frame in wide format.


Example 2: Reshaping Data Frame from Long to Wide Format Using spread Function of tidyr Package

The tidyverse is a powerful environment for data manipulation in R (packages such as dplyr and ggplot2 are part of the tidyverse) and also provides functions for the conversion of narrow to wide data.

This example shows how to use the tidyr package of the tidyverse to do this. If we want to use the functions of the tidyr package, we first have to install and load tidyr:

install.packages("tidyr")                                      # Install & load tidyr package

Now, we can use the spread function provided by the tidyr package to reshape our data frame:

data_reshape2 <- spread(data,                                  # Applying spread function
                        key = group_2,
                        value = values)
data_reshape2                                                  # Print wide data frame
#   group_1 D E F
# 1       A 1 2 3
# 2       B 4 5 6
# 3       C 7 8 9

Even though the column names of the final output are different, the values are exactly the same compared to Example 1.

Update: Please note that the development on the spread function is complete, and for new code it is recommended to use the pivot_wider function. More information on how to use pivot_wider can be found here.


Video & Further Resources

Have a look at the following video of my YouTube channel. I’m explaining the R programming code of this tutorial in the video.


The YouTube video will be added soon.


Also, you could have a look at the related posts of my website. You can find some articles below.


In summary: In this R tutorial you learned how to reshape long data frames to wide format. In case you have any additional questions and/or comments, please tell me about it in the comments.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.