liveBook · Manning

Pivot_wider() function

As explained in Statistics Slam Dunk

The pivot_wider() function from tidyr reshapes long-format data into wide format by turning values of a variable into columns.

pivot_wider() reshapes long-format data into wide format by turning values from one variable into column names and filling those columns with values from another variable. In NBA inequality analysis, it converts player-level salaries or win shares into separate columns for each team-season combination. section 12.7.1 section 13.8.1

A typical transformation prepares grouped observations before widening them:

  • Remove helper variables that are no longer needed, such as rank. section 12.7.1
  • Group by the variables that should identify each resulting row, such as team and season. section 13.8.1
  • Create an independent sequence within each group with row_number(), then use that sequence in names_from. section 12.7.1
  • Supply values_from to determine which measurements populate the new cells. section 13.6.2

Turn team win shares into columns

ws_gini2 %>% select(-c(rank)) %>% group_by(Tm, Year) %>% mutate(id = row_number(WS)) %>% pivot_wider(names_from = id, values_from = WS) -> ws_gini3 The resulting tibble keeps Year and Tm as identifiers and creates numbered columns containing each team's win shares. Renaming those columns to ws1 through ws14 makes their later use clearer. The same pattern reshapes salaries with team and season_end as identifiers and salary as the cell values. section 13.8.1 section 12.7.1

ws_gini2 %>%
 select(-c(rank)) -> ws_gini2

ws_gini2 %>%
 group_by(Tm, Year) %>%
 mutate(id = row_number(WS)) %>%
 pivot_wider(names_from = id, values_from = WS) -> ws_gini3
head(ws_gini3)
## # A tibble: 6 × 16
## # Groups:   Tm, Year [6]
##   Year  Tm      `1`   `2`   `3`   `4`   `5`   `6`
##   <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1991  ATL     0     0     0     0     1.1   1.8
## 2 1991  BOS     0     0     0     0     0.3   1.2
## 3 1991  CHH     0     0.1   0.3   0.6   1.8   1.8
## 4 1991  CHI     0.5   0.9   1.5   1.7   2     2.3
## 5 1991  CLE     0     0     0.2   0.6   1     1.3
## 6 1991  DAL     0.1   0.4   0.6   0.6   0.6   0.7
##    `7`   `8`   `9`  `10`  `11`  `12`  `13`  `14`
##   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1.9   2.5   4     4.4   5.4   5.6   6.3  11.4
## 2 3.5   4.7   5.9   6.6   7.5   7.9   8.3  10
## 3 2     2     2.1   2.7   3     3     4.1   4.8
## 4 4.1   4.2   5.7  10.3  11.2  20.3  NA    NA  
## 5 1.5   1.6   2     2.4   3.1   3.4   9     9.8
## 6 0.7   1     1.6   1.7   4.3   5.3   5.5   7.6

Inspect the complete grouped pipeline and resulting 16-column tibble, including the numbered win-share columns created by pivot_wider(). section 13.8.1

How Statistics Slam Dunk covers it

How deep it goes
Explains names_from, values_from, grouping, and helper identifiers through executable pipelines, but defers broader pivoting options.
Examples
Uses team-season salary columns, team-season win-share columns, and four year columns for Lorenz-curve preparation.
What it assumes
Assumes familiarity with tibbles, pipelines, and grouping while demonstrating the reshaping workflow from raw long observations.
What you can do after
Apply it via tidyr to convert grouped long data into analysis-ready wide tibbles.
How it connects
Connects long format, wide format, group_by(), mutate(), row_number(), Gini coefficient, and joins.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.