liveBook · Manning

Slice

As explained in Statistics Slam Dunk

The dplyr slice() function selects rows from a tabular object by their positions, including positions computed within groups.

In dplyr, slice() selects rows from a tabular object by position. Group the data first to apply that positional selection separately within each group, such as removing the first record from every date or retaining each year's maximum row. section 16.4.2 section 19.4

Keep each year's scoring leader

dat2 %>% group_by(Year) %>% slice(which.max(PPG)) -> dat3 selects the observation with the highest points-per-game value in every year. Unlike summarize(), slice() retains every variable from each selected record, including Year, Player, PPG, and Year_Player. section 19.4 section 19.4.1

dat2 %>%
 group_by(Year) %>%
 slice(which.max(PPG)) -> dat3

Inspect the grouped slice() pipeline that keeps the maximum PPG observation for each Year. section 19.4

Remove the first record in each game

randle_final %>% group_by(date) %>% slice(-1) -> randle_final removes the first free-throw record within every date group, reducing the dataset from 350 to 289 records. section 16.4.2

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates grouped deletion, maximum-row selection, and row preservation, but does not detail broader positional-selection variants.
Examples
Uses Julius Randle free throws and year-by-year NBA scoring leaders as worked row-selection examples.
What it assumes
Assumes familiarity with tabular data, pipes, and group_by(), while demonstrating grouped selection through concrete NBA datasets.
What you can do after
Apply it via dplyr to remove grouped rows or retain complete records meeting grouped positional criteria.
How it connects
Connects row selection with dplyr pipes, group_by(), which.max(), summarize(), mutate(), filter(), and ggplot2.

Also explained in

Compare how 2 books explain slice →

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.