liveBook · Manning

Arrange() function

As explained in Statistics Slam Dunk

The arrange() function from dplyr orders rows of a tabular object according to one or more columns or expressions.

arrange() from dplyr orders the rows of a tabular object by one or more variables or expressions. It fits naturally inside a pipe workflow: sort the data, group observations, create ranks, and filter the rows needed for analysis. section 12.4 section 13.3

Use arrange() to prepare NBA data for focused comparisons:

  • Sort salary records by season, team, and salary before ranking players within each season-team group. section 12.4
  • Sort win-share records by Year, team abbreviation, and WS before retaining no more than 14 players per group. section 13.3
  • Sort championship-team records by season to create an orderly regular-season comparison. section 14.7

Keep the top 14 salaries per team and season

gini3 %>% arrange(season_end, team, salary) %>% group_by(season_end, team) %>% mutate(rank = rank(-salary, ties.method = "first")) %>% filter(rank <= 14) -> gini4 Here, arrange() establishes a consistent season-team-salary order. rank() then ranks salary in descending order because of the negative sign, and filter() keeps the first 14 ranked records in each group. section 12.4

gini3 %>%
 arrange(season_end, team, salary) %>%
 group_by(season_end, team) %>%
 mutate(rank = rank(-salary, ties.method = "first")) %>%
 filter(rank <= 14) -> gini4

Inspect the complete pipeline that combines arrange() with grouped ranking and filtering. section 12.4

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates multi-variable sorting in complete NBA data-preparation pipelines without detailing implementation internals.
Examples
Uses NBA salaries, win shares, seasons, teams, and top-14 player selection.
What it assumes
Assumes a tabular object and basic pipe-based data manipulation with dplyr.
What you can do after
Apply it via dplyr to order rows before grouped ranking and maximum-record filtering.
How it connects
Connects sorting with the pipe operator, group_by(), mutate(), rank(), and filter().

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.