A derived variable is a new data variable computed from existing variables rather than recorded directly. It can simplify messy values, combine fields, calculate a rate or total, or turn a continuous measure into useful analysis groups. Create it when the original columns do not directly answer the question you want to investigate. section 2.3.5 section 2.4
In R, `dplyr::mutate()` commonly appends a derived variable to a data frame. The calculation can use logical conditions, arithmetic, string extraction, concatenation, or recoding. When a result has a fixed set of categories, convert it to a factor so grouping and categorical analysis treat it appropriately. section 2.3.5 section 14.2.2
Common patterns include:
- Recode detailed labels into broader categories, such as country of birth into USA or World, or multi-position labels into Center, Big, Forward, Guard, and Swingman. section 2.3.5
- Create an indicator from missingness or a condition, such as whether a player attended college. section 2.3.5
- Bin a numeric variable into segments, such as draft picks 1–5 through 26–30. section 3.4
- Calculate quantities from multiple columns, such as total salaries, total wins, salary per win, or mean wins. section 11.4.1
- Derive a value through arithmetic, such as points allowed per game from points scored and point differential. section 14.2.4
- Extract or combine text for readable labels, such as a season from `w2017` or a combined year-and-player chart label. section 14.2.2 section 19.4
Turn draft pick numbers into analysis segments
A raw draft-pick variable runs from 1 to 30, which is too detailed for a six-group comparison. Create `Pk2` with nested `ifelse()` calls and `%in%` so each pick falls into a range such as `1-5` or `6-10`; values outside the expected range become `NA`. Convert `Pk2` to a factor before calculating segment statistics or plotting results. section 3.4
mutate(draft2, Pk2 = ifelse(Pk %in% 1:5, "1-5",
ifelse(Pk %in% 6:10, "6-10",
ifelse(Pk %in% 11:15, "11-15",
ifelse(Pk %in% 16:20, "16-20",
ifelse(Pk %in% 21:25, "21-25",
ifelse(Pk %in% 26:30, "26-30", "NA"))))))) -> draft2
This listing builds six draft-pick segments from the original numeric `Pk` variable using `mutate()`, `%in%`, and nested conditions. section 3.4
Build a team efficiency measure
For each team, sum salary columns and win columns with `rowSums()`, then divide total salary by total wins. The resulting `efficiency` variable condenses 16 seasons of salary and performance data into one value for comparison. A related `meanWins` variable divides total wins by the number of seasons and rounds the result. section 11.4.1
cha %>%
mutate(sumSalaries = rowSums(.[2:17]),
sumWins = rowSums(.[18:33]),
efficiency = rowSums(.[2:17]) / rowSums(.[18:33]),
meanWins = round(rowSums(.[18:33]) / 16)) -> cha
This listing creates four team-level derived variables: salary total, win total, salary-per-win efficiency, and average wins. section 11.4.1
Derived variables should preserve a clear relationship to their inputs and fit the next analytical task. For example, categorical point-scoring and points-allowed variables are created from standardized values because ANOVA requires a categorical predictor; a binary derived target can prepare data for logistic regression. Check the new variable after creating it: inspect levels, counts, missing values, and a few records before modeling or plotting. section 14.5.1 section 14.6

