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
