“How can we use the pivot_wider() function in R to reshape our data and make it wider?”

“How can we use the pivot_wider() function in R to reshape our data and make it wider?”

The pivot_wider() function in R allows us to reshape our data and make it wider by pivoting the data from a longer format to a wider format. This function helps to rearrange data by converting rows into columns, making it easier to analyze and visualize data in a more organized manner. By specifying the appropriate columns and values, the pivot_wider() function can efficiently create a new data frame with wider dimensions, allowing for easier comparison and manipulation of data. This function is a useful tool in data analysis and can greatly improve the efficiency of data management and presentation.

Use pivot_wider() in R


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

This function uses the following basic syntax:

library(tidyr)

df %>% pivot_wider(names_from = var1, values_from = var2)

where:

  • names_from: The column whose values will be used as column names
  • values_from: The column whose values will be used as cell values

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

Related:

Example: Use pivot_wider() in R

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(player=rep(c('A', 'B'), each=4),
                 year=rep(c(1, 1, 2, 2), times=2),
                 stat=rep(c('points', 'assists'), times=4),
                 amount=c(14, 6, 18, 7, 22, 9, 38, 4))

#view data frame
df

  player year    stat amount
1      A    1  points     14
2      A    1 assists      6
3      A    2  points     18
4      A    2 assists      7
5      B    1  points     22
6      B    1 assists      9
7      B    2  points     38
8      B    2 assists      4

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

library(tidyr)

#pivot the data frame into a wide format
df %>% pivot_wider(names_from = stat, values_from = amount)

# A tibble: 4 x 4
  player  year points assists
         
1 A          1     14       6
2 A          2     18       7
3 B          1     22       9
4 B          2     38       4

Notice that the values from the stat column are now used as column names and the values from the amount column are used as cell values in these new columns.

The final result is a wide data frame.

Note: You can find the complete documentation for the pivot_wider() 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 we use the pivot_wider() function in R to reshape our data and make it wider?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-use-the-pivot_wider-function-in-r-to-reshape-our-data-and-make-it-wider/

stats writer. "“How can we use the pivot_wider() function in R to reshape our data and make it wider?”." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-we-use-the-pivot_wider-function-in-r-to-reshape-our-data-and-make-it-wider/.

stats writer. "“How can we use the pivot_wider() function in R to reshape our data and make it wider?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-use-the-pivot_wider-function-in-r-to-reshape-our-data-and-make-it-wider/.

stats writer (2024) '“How can we use the pivot_wider() function in R to reshape our data and make it wider?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-use-the-pivot_wider-function-in-r-to-reshape-our-data-and-make-it-wider/.

[1] stats writer, "“How can we use the pivot_wider() function in R to reshape our data and make it wider?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. “How can we use the pivot_wider() function in R to reshape our data and make it wider?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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