`summarize()` from dplyr reduces data to one or more summary values. Supply named calculations such as `mean(WS)`, `median(WS)`, `min(WS)`, `max(WS)`, or `sum(PTS)`; the names become columns in the result. The output is commonly a tibble, ready for further wrangling or a ggplot2 chart. section 2.5.3 section 19.3.1
Without grouping, calculations describe the whole input. Put `group_by()` earlier in a pipe to repeat them for every group: each group becomes a row in the result. This makes `summarize()` useful for questions such as how average career win shares vary by draft year, birthplace, college attendance, position, or draft-pick segment. section 2.5.3 section 2.5.5
A practical workflow is:
- Start with the data, and optionally use `filter()` or `select()` to retain relevant records or columns. section 8.5.1 section 19.3.1
- Use `group_by()` for the categories that should receive separate summaries. section 3.5.1
- Call `summarize()` with named functions, then save or plot the resulting tibble. section 2.5.5 section 18.4.4
Compare career win shares by birthplace
Grouping NBA first-round selections by `Born2` and summarizing `mean(WS)` and `median(WS)` produces two rows: USA players average 31.1 career win shares with a 21.5 median, while World players average 23.5 with a 12.8 median. Those two measures can then be drawn as side-by-side bar charts. section 2.5.5
draft %>%
group_by(Born2) %>%
summarize(meanWS = mean(WS),
medianWS = median(WS)) -> tenth_tibble
print(tenth_tibble)
## # A tibble: 2 × 3
## Born2 meanWS medianWS
## <fct> <dbl> <dbl>
## 1 USA 31.1 21.5
## 2 World 23.5 12.8
This listing shows the compact pattern: group by a category, calculate multiple named summaries, and inspect the resulting tibble before visualization. section 2.5.5
`summarize()` can also consolidate records rather than merely describe them. After removing total-team rows, grouping player-season-position records and summing games and points merges multiple team stints into one record; columns not used as grouping keys or summary expressions do not remain in the result. section 19.3.1
