liveBook · Manning

Select() function

As explained in Statistics Slam Dunk

The select() function is a dplyr function that selects or deselects data-frame columns by name, position, or selection expression.

`select()` from dplyr reshapes a data frame’s column set: retain needed variables, drop unneeded ones, or arrange the remaining columns in a new order. It is a practical first step when wide NBA data contains fields that do not serve the analysis. section 2.3.1 section 14.2.4

Pass column names to keep them, or prefix selections with `-` to remove them. Columns can also be selected by position, contiguous ranges, or helpers such as `starts_with()`. Naming columns is generally preferred to positions, unless names create errors or many columns must be removed. section 2.3.1 section 9.3

Use `select()` to prepare a focused data set for the next operation:

  • Keep a small analytical set, such as `select(DATASET, TEAM, VENUE, FT, FTA, PF)`, before renaming and combining road and home records. section 7.3
  • Keep a consecutive run of columns with `select(DATASET:MIN)` rather than listing every variable individually. section 6.3.2
  • Drop unwanted ranges with `select(-c(OT1R:OT5R, MINR, OT1H:OT5H, MINH))`. section 6.3.2
  • Reorder retained columns by passing all of them in the desired sequence, such as `select(Team, season, PTS, O_PTS, PTS_DIFF)`. section 14.2.4

Trim a draft data set

To remove superfluous career statistics by position, pipe the data frame into `select()` and use a negative selection. The result retains every column except positions 3, 4, and 16 through 24. section 2.3.1

draft <- select(draft,-c(3,4,16:24))

Inspect a minimal executable example of negative positional selection with `select()`. section 2.3.1

How Statistics Slam Dunk covers it

How deep it goes
Defines inclusion, exclusion, names, positions, ranges, helpers, and reordering through repeated executable transformations.
Examples
Demonstrates draft statistics, hustle metrics, game records, salaries, play-by-play events, and betting data.
What it assumes
Introduces selection from zero while using R data frames, the pipe operator, and dplyr function calls.
What you can do after
Apply it via dplyr to retain, remove, range-select, pattern-select, and reorder data-frame columns.
How it connects
Connects to filter(), rename(), mutate(), pivot_longer(), joins, correlation, and K-means clustering.

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.