liveBook · Manning

Filtering

As explained in Statistics Slam Dunk

The filter() function is a dplyr function that returns rows of a tabular object satisfying specified logical conditions.

Filtering returns the rows of a tabular data set that satisfy logical conditions. In dplyr, use filter() to keep observations matching a value, comparison, or combination of conditions, then save the smaller result for analysis. Filtering changes rows, not columns; use select() when the goal is to retain fewer variables. section 6.3.1 section 7.3

Write == for “equals” inside a condition: = assigns a value instead. Combine conditions with & when every condition must be true, | when at least one can be true, and != to exclude a value. Conditions can compare a variable with a number, text label, or another variable. section 6.3.1 section 6.4.5 section 6.4.6

Keep regulation regular-season games

To analyze comparable games, retain only records labeled as regular season with 240 player-minutes. The result removes playoff games and overtime games; dim() then verifies that the data set has fewer rows. section 6.3.1

nba1819 %>%
 filter(DATASET == "NBA 2018-2019 Regular Season" & MIN == 240) -> nba1819
dim(nba1819) 
## [1] 2326   57

This pipeline filters nba1819 to regulation regular-season records and confirms the resulting row and column count. section 6.3.1

Common filtering jobs include:

  • Split paired game records into road and home data sets before renaming and joining them. section 6.3.2
  • Keep only selected teams or quarter-result categories before counting and comparing outcomes. section 6.4.5
  • Remove playoffs, technical free throws, aggregate team records, or incomplete seasons when they do not fit the question. section 8.4 section 16.2
  • Restrict numeric ranges, such as possessions lasting 5 through 24 seconds, or retain ranked records with rank <= 14. section 8.5.1 section 15.3
  • Create a repeatable train/test split by selecting rows whose row number meets a modular condition. section 14.6.1

Filtering is often an early wrangling decision with analytical consequences. Excluding postseason games can make schedules comparable; removing duplicate aggregate team records prevents a player’s multi-team season from being counted as a separate total; dropping sparse early salary seasons avoids building inequality measures on incomplete records. Check row counts and inspect the retained variables before continuing. section 8.4 section 12.4 section 13.3

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates equality, inequality, ranges, AND/OR logic, variable comparisons, and row-number conditions, while deferring broader query syntax.
Examples
Uses regulation games, home-road splits, top teams, tied halftime games, possessions, free throws, salaries, and win shares.
What it assumes
Assumes tabular R data and introduces the distinction between assignment and equality alongside basic logical operators.
What you can do after
Apply it via dplyr filter() to retain or exclude rows using logical conditions and verify the resulting data dimensions.
How it connects
Connects filtering with dplyr, select(), mutate(), count(), group_by(), joins, factors, and train-test splits.

Also explained in

Compare how 33 books explain filtering →

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.