liveBook · Manning

Rank function

As explained in Statistics Slam Dunk

The base R rank() function assigns ordinal ranks to elements of a vector according to their values and tie-handling rules.

The base R rank() function assigns ordinal positions to values. Combine it with grouping to rank each team’s players separately, and use a negative value to rank from largest to smallest. section 12.4 section 13.3

Keep the top 14 players in every group

gini3 %>% arrange(season_end, team, salary) %>% group_by(season_end, team) %>% mutate(rank = rank(-salary, ties.method = "first")) %>% filter(rank <= 14) -> gini4 The same pattern ranks win shares within each Year and Tm combination. arrange() prepares the data, group_by() defines independent ranking groups, mutate() stores the ranks, and filter() retains the first 14 records. section 12.4 section 13.3

The ties.method = "first" setting gives equal values consecutive, different ranks rather than sharing one rank. This guarantees that filtering rank <= 14 returns no more than 14 records per group, even when values are tied. section 12.4 section 13.3

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 salary-ranking pipeline used to create grouped ranks and retain the top 14 players. section 12.4

How Statistics Slam Dunk covers it

How deep it goes
Explains descending order, grouping, tie handling, filtering, and integrity checks, but does not develop rank() internals or alternative tie methods.
Examples
Demonstrates rankings for NBA salaries and win shares, including tied Atlanta win shares and team-specific validation queries.
What it assumes
Assumes familiarity with data frames, pipes, sorting, grouping, and filtering in dplyr.
What you can do after
Apply it via R and dplyr to rank observations within groups and retain a controlled top-N subset.
How it connects
Connects rank() with dplyr arrange(), group_by(), mutate(), and filter() for grouped data preparation.

Also explained in

Compare how 2 books explain rank function →

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.