liveBook · Manning

$ operator

As explained in Statistics Slam Dunk

The $ operator in R extracts or subsets a named component, such as a variable, from a data object.

Use the $ operator to extract or subset a named variable from a data object. It also lets you assign a transformed version back to that variable, as in draft$Year <- as.factor(draft$Year). section 2.3.4

Apply the same pattern when preparing NBA data for analysis:

  • Convert selected draft variables, including Year, Tm, Born, From, and To, to factors. section 2.3.4
  • Create play_length2 from play_length, then convert it to numeric with pbp$play_length2 <- as.numeric(pbp$play_length2). section 8.4
  • Inspect only one variable with head(pbp$play_length), rather than returning every column in the data set. section 8.4

Convert event types before counting them

Turn the character variable into a factor with pbp$event_type <- as.factor(pbp$event_type), inspect its levels with levels(pbp$event_type), and then count observations by event_type. section 8.4

draft$Year <- as.factor(draft$Year)
draft$Tm <- as.factor(draft$Tm)
draft$Born <- as.factor(draft$Born)
draft$From <- as.factor(draft$From)
draft$To <- as.factor(draft$To)

Review a compact listing of repeated $ assignments that convert selected draft variables to factors. section 2.3.4

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates extraction, reassignment, inspection, and conversion, without explaining the operator's implementation internals.
Examples
Uses draft variables Year and Tm plus play-by-play variables play_length, event_type, team, and points.
What you can do after
Apply it via R to select individual variables, inspect their values, and assign converted or newly derived variables.
How it connects
Connects named-variable access with factor variables, as.factor(), class(), glimpse(), head(), and grouped data exploration.

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.