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

