liveBook · Manning

Pipe operator

As explained in Statistics Slam Dunk

The R pipe operator passes the result of one expression as an input to a subsequent function call.

The R pipe operator, written %>%, sends the output of one operation to the next function as an argument. Read it as “then”: start with a data set, then transform, summarize, or filter it. This makes a sequence of data-wrangling decisions easier to read than a disconnected set of intermediate steps. section 2.5.3

A pipeline begins with a data object and continues one line at a time. Each function receives the result produced above it, while the final result can be assigned to a tibble for printing, plotting, or further analysis. Tibbles behave much like data frames, although their printed display is abbreviated. section 2.5.3

Use a pipe when an analysis naturally has ordered steps:

  • Summarize an entire draft data set by piping it into summarize() to calculate minimum, quartiles, mean, median, and maximum win shares. section 2.5.3
  • Group draft picks by year, then summarize average win shares for every draft class. section 2.5.3
  • Filter players by a win-share threshold, group the remaining rows by year, then compute the average draft pick. section 2.5.3
  • Arrange salary records, group them by season and team, rank salaries, and retain the top 14 players per group. section 12.4
  • Select continuous draft variables before passing the resulting object to cor() for correlation analysis. section 2.5.4

Summarize a draft class pipeline

draft %>% group_by(Year) %>% summarize(avg = mean(WS)) -> second_tibble The pipeline starts with draft, groups its rows by Year, and then calculates mean win shares within each group. The result is stored in second_tibble, producing one average for each draft year. section 2.5.3

Keep the 14 highest salaries per team-season

gini3 %>% arrange(season_end, team, salary) %>% group_by(season_end, team) %>% mutate(rank = rank(-salary, ties.method = "first")) %>% filter(rank <= 14) -> gini4 This chain sorts salary data, defines groups by season and team, creates descending salary ranks, and filters each group to no more than 14 records. 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

Follow the complete pipeline that sorts, groups, ranks, and filters salary records into a working tibble. section 12.4

How Statistics Slam Dunk covers it

How deep it goes
Defines output passing and demonstrates multi-step pipelines with filtering, grouping, ranking, selection, and summarization.
Examples
Uses NBA draft win shares, draft position thresholds, team-season salaries, and continuous-variable correlation data.
What it assumes
Teaches the pipe’s basic output-to-input role while introducing dplyr functions and tibble results.
What you can do after
Apply it via dplyr to chain data transformations and save the resulting tibble for analysis or visualization.
How it connects
Connects dplyr, tibble, summarize(), group_by(), filter(), arrange(), mutate(), select(), rank(), and correlation.

Also explained in

Compare how 6 books explain pipe operator →

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.