liveBook · Manning

Derived variable

As explained in Statistics Slam Dunk

A derived variable is a data variable computed from one or more existing variables rather than directly recorded in the original dataset.

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

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates conditional, arithmetic, string, and aggregate derivations, including factor conversion and validation checks.
Examples
Uses birth country, college attendance, positions, draft ranges, team salaries and wins, seasons, points allowed, and points per game.
What it assumes
Introduces logical tests, missing values, factors, and R assignment while using mutate(), ifelse(), and recode().
What you can do after
Apply it via R functions to create categorical, numeric, text, and aggregate variables for later analysis.
How it connects
Connects data wrangling with factors, missing values, segmentation analysis, data standardization, ANOVA, logistic regression, and K-means clustering.

Also explained in

Compare how 2 books explain derived variable →

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.