How can I use pivot_longer() in R for data manipulation?

How can I use pivot_longer() in R for data manipulation?

Pivot_longer() is a function in R that allows for efficient data manipulation by transforming a dataset from wide to long format. This function essentially reshapes the data, creating a longer and narrower dataset, making it easier to analyze and work with. By specifying the columns to pivot and the values to be converted into new columns, pivot_longer() helps restructure the data into a more organized and readable format. This function is particularly useful for data analysis and visualization purposes, providing a convenient way to access and manipulate data in a versatile manner.

Use pivot_longer() in R


The pivot_longer() function from the 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:

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 .

Additional Resources

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

Cite this article

stats writer (2024). How can I use pivot_longer() in R for data manipulation?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-pivot_longer-in-r-for-data-manipulation/

stats writer. "How can I use pivot_longer() in R for data manipulation?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-pivot_longer-in-r-for-data-manipulation/.

stats writer. "How can I use pivot_longer() in R for data manipulation?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-pivot_longer-in-r-for-data-manipulation/.

stats writer (2024) 'How can I use pivot_longer() in R for data manipulation?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-pivot_longer-in-r-for-data-manipulation/.

[1] stats writer, "How can I use pivot_longer() in R for data manipulation?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use pivot_longer() in R for data manipulation?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top