liveBook · Manning

%in% operator

As explained in Statistics Slam Dunk

The %in% operator in R tests whether each element occurs in a specified vector or data frame.

The %in% operator tests whether each element occurs in a specified vector or data frame. It returns a logical result that can guide conditional assignment or row filtering. section 3.4 section 12.4

Group NBA draft picks into segments

To turn the numeric Pk variable into six draft-selection groups, test membership in ranges: `ifelse(Pk %in% 1:5, "1-5", ifelse(Pk %in% 6:10, "6-10", ...))`. Values from 1 through 30 receive a category; other values become `NA`. Convert the resulting categorical variable with `as.factor(Pk2)` before computing statistics or creating visualizations. section 3.4

Remove incomplete seasons

Create a vector of unwanted season levels and negate the membership test: `gini[!(gini$season_end %in% c(1985, 1986, 1987, 1988, 1989, 1990)), ] -> gini3`. The `!` changes membership into exclusion, reducing the data from 14,163 to 12,929 records. section 12.4

How Statistics Slam Dunk covers it

How deep it goes
Explains vector membership, conditional branching, negation, and two practical NBA data-filtering applications.
Examples
Demonstrates draft-pick ranges and exclusion of NBA seasons 1985 through 1990.
What you can do after
Apply it to assign categories conditionally or filter rows by retaining or excluding matching values.
How it connects
Connects membership testing with vectors, data frames, ifelse(), logical negation, filtering, mutate(), and factor variables.

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.