pivot_longer & pivot_wider Functions of tidyr Package in R (2 Examples)

 

In this R tutorial you’ll learn how to apply the pivot_longer and pivot_wider functions of the tidyr add-on package.

The content of the page looks like this:

Let’s start right away.

 

Example Data & Add-On Packages

Consider the following example data:

data <- data.frame(ID1 = LETTERS[1:4],        # Create example data
                   ID2 = rep(letters[1:3], each = 4),
                   x = 1:12,
                   y = 21:32)
data                                          # Print example data

 

table 1 data frame pivot_long and pivot_wide functions r

 

Table 1 visualizes the output of the RStudio console returned by the previous R programming code and visualizes the structure of our example data – It is constituted of twelve rows and four columns.

The variables ID1 and ID2 contain id values that we will use to reshape our data frame. Note that our example data frame has a wide format.

For the examples of this tutorial, we also need to install and load the tidyr package:

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

Now, we are set up and can move on to the examples!

 

Example 1: Convert Wide to Long Data Using pivot_longer() Function

Example 1 shows how to reshape our data frame from wide to long format using the pivot_longer function of the tidyr package.

For this, we have to specify the name of our data frame (i.e. data) and the columns that we want to pivot into longer format (i.e. x and y):

data_long <- pivot_longer(data = data,        # Convert wide to long
                          cols = c("x", "y"))
data_long                                     # Print long data

 

table 2 tbl_df pivot_long and pivot_wide functions r

 

In Table 2 it is shown that we have created a data set in long format by executing the previous R syntax.

Please note that the output of the pivot_longer function is a tibble, even though we have used a data frame as input. In case you prefer to work with data frames, you have to use the as.data.frame function to convert the tibble back to the data.frame class.

 

Example 2: Convert Long to Wide Data Using pivot_wider() Function

This example shows how to pivot our long data set that we have created in Example 1 to a wide data format.

For this, we can use the pivot_wider function. Within the pivot_wider function, we only have to specify our long data as shown below:

data_wide <- pivot_wider(data = data_long)    # Convert long to wide
data_wide                                     # Print wide data

 

table 3 tbl_df pivot_long and pivot_wide functions r

 

Table 3 shows the output of the previous code – A tibble in wide data format.

Note that the structure of Table 3 is exactly the same as Table 1 (i.e. our wide input data frame).

 

Video & Further Resources

In case you need more info on the R syntax of this tutorial, you may want to watch the following video tutorial of my YouTube channel. I illustrate the R syntax of this article in the video:

 

 

Additionally, you may want to read some of the related articles on my website. I have released numerous tutorials already:

 

Summary: You have learned in this tutorial how to reshape data sets using pivot_longer and pivot_wider in the R programming language.

Note that pivot_longer and pivot_wider are also replacing older functions of the tidyverse such as gather and spread. So it’s definitely worth to add those functions to your repertoire!

In case you have any additional comments and/or questions, tell me about it in the comments below. Furthermore, please subscribe to my email newsletter to receive updates on the newest tutorials.

 

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.


4 Comments. Leave new

  • Can you expand this to a situation where there is more than one time varying variable we wish to convert to long. For example, say you have blood pressure (BP) and heart rate (HR) recording at three time periods for each participant. In wide format it would look something like:

    ID BP1 BP2 BP3 HR1 HR2 HR3
    01 120 125 130 72 74 70

    In long format we would like this:

    ID Time BP HR
    01 1 120 72
    01 2 125 74
    01 3 130 70

    Reply
    • Hey Joe,

      Thank you for the interesting question, and sorry for the delayed response, I just came back from vacation. Do you still need help with this?

      Regards,
      Joachim

      Reply
      • Yes, I need help with this. Please can you elaborate.

        Reply
        • Hey,

          Please have a look at the example code below:

          data <- data.frame(ID = 0:1,
                             BP1 = c(120, 130),
                             BP2 = c(125, 120),
                             BP3 = c(130, 135),
                             HR1 = c(72, 75),
                             HR2 = c(74, 70),
                             HR3 = c(70, 76))
          data
          #   ID BP1 BP2 BP3 HR1 HR2 HR3
          # 1  0 120 125 130  72  74  70
          # 2  1 130 120 135  75  70  76
           
          data_new <- data.frame(ID = data$ID,
                                 BP = c(data$BP1, data$BP2, data$BP3),
                                 HR = c(data$HR1, data$HR2, data$HR3))
          data_new <- data_new[order(data_new$ID), ]
          data_new
          #   ID  BP HR
          # 1  0 120 72
          # 3  0 125 74
          # 5  0 130 70
          # 2  1 130 75
          # 4  1 120 70
          # 6  1 135 76

          Regards,
          Joachim

          Reply

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.

Top