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





