3.1 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Use pivot_longer() in R

How to Use pivot_longer() in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The pivot_longer() function from the tidyr package in R can be used to pivot a data frame from a wide format to a long format.

This function uses the following basic syntax:

library(tidyr)

df %>% pivot_longer(cols=c('var1', 'var2', ...),
                    names_to='col1_name',
                    values_to='col2_name') 

where:

  • cols: The names of the columns to pivot
  • names_to: The name for the new character column
  • values_to: The name for the new values column

The following example shows how to use this function in practice.

Related: Long vs. Wide Data: What’s the Difference?

Example: Use pivot_longer() in R

Suppose we have the following data frame in R that shows the number of points scored by various basketball players in different years:

#create data frame
df frame(player=c('A', 'B', 'C', 'D'),
                 year1=c(12, 15, 19, 19),
                 year2=c(22, 29, 18, 12))

#view data frame
df

  player year1 year2
1      A    12    22
2      B    15    29
3      C    19    18
4      D    19    12

We can use the pivot_longer() function to pivot this data frame into a long format:

library(tidyr)

#pivot the data frame into a long format
df %>% pivot_longer(cols=c('year1', 'year2'),
                    names_to='year',
                    values_to='points')

# A tibble: 8 x 3
  player year  points
      
1 A      year1     12
2 A      year2     22
3 B      year1     15
4 B      year2     29
5 C      year1     19
6 C      year2     18
7 D      year1     19
8 D      year2     12

Notice that the column names year1 and year2 are now used as values in a new column called “year” and the values from these original columns are placed into one new column called “points.”

The final result is a long data frame.

Note: You can find the complete documentation for the pivot_longer() function here.

Additional Resources

The following tutorials explain how to use other common functions in the tidyr package in R:

How to Use pivot_wider() in R
How to Use Spread Function in R
How to Use Gather Function in R
How to Use Separate Function in R
How to Use the Unite Function in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories