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')
- 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 <- data.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.
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