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
