Categorical data places each observation into one of a fixed set of groups rather than measuring an amount. NBA examples include a player’s birth region, position, draft-pick segment, team, game venue, season, event type, and whether a free throw was made or missed. In R, these variables are commonly stored as factors. section 2.3.4 section 16.2
A value that looks numeric can still be categorical when its possible values are finite and represent groups. Draft year is treated as a factor because the data spans only 2000 through 2009; team likewise has a bounded set of possibilities. Converting genuine categories to factors is a best practice before modeling or visualization. section 2.3.4 section 6.3.2
Prepare categorical data by making the groups explicit:
- Inspect variable classes with tools such as glimpse() and confirm a factor with class(). section 2.3.3 section 2.3.4
- Convert a categorical character or numeric column with as.factor(); use levels() to inspect its available groups. section 2.3.4 section 8.4
- Create useful groups from raw values, then convert the derived result to a factor. section 2.3.5 section 3.4
- Check missing or inconsistent labels before grouping, filtering, or summarizing. section 2.3.5 section 8.4
Turn positions into analysis-ready groups
Raw player positions include C, C-F, F, F-C, F-G, G, and G-F. Recode them into broader labels such as Center, Big, Forward, Guard, and Swingman, then store both the original and grouped position variables as factors. This preserves the detailed labels while creating categories suitable for grouped analysis. section 2.3.5
Factors support analyses organized by levels. Grouping play-by-play records by event type produces counts for fouls, rebounds, shots, and other events. Factor levels can also define segments for boxplots, while categorical predictors can be paired with quantitative outcomes in ANOVA. section 8.4 section 14.5.1
draft$Pos2 <- draft$Pos
draft$Pos2 <- recode(draft$Pos2,
"C" = "Center",
"C-F" = "Big",
"F" = "Forward",
"F-C" = "Big",
"F-G" = "Swingman",
"G" = "Guard",
"G-F" = "Swingman")
draft$Pos <- as.factor(draft$Pos)
draft$Pos2 <- as.factor(draft$Pos2)
levels(draft$Pos2)
## [1] "Big" "Center" "Forward" "Guard" "Swingman"
Compare detailed position codes with the broader recoded factor levels used for analysis. section 2.3.5















