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
































