13  Transforming Your Data

How to use dplyr

Authors

Mara Estreich

Shane McCarty

Published

10.05.2026

Abstract

This chapter introduces researchers to dplyr, the tidyverse package for transforming data. Researchers learn to recode variables and create new categorical and binary variables with mutate(), case_when(), if_else(), and factor(), and to name what they make with the class convention (_#CAT for a category with # levels, _01CAT for a binary, both reserved for variables created in R). The Play collapses seven education categories into three, cuts the ten-item K10 into its published bands, and combines the select-all RACIALIZED boxes into one variable. The Lab practices the same moves on the lab dataset, which from this chapter on is loaded with source(“lab_prep.R”) (EDUCATION_3CAT, POL_3CAT, HEALTH_3CAT, SES_01), with Ref’s checks, and Your Turn gives a template for the team dataset.

Keywords

data manipulation, dplyr, recoding variables, creating new variables, case_when

Open Project → Open .qmd → Run load-library chunk → Run All Chunks Above → code. If anything looks wrong, use Ref’s Quick Checklist.

library(haven)
library(naniar)
library(dplyr)
library(knitr)

13.1 Introduction to dplyr

dplyr is part of the tidyverse and provides intuitive functions for manipulating data. Think of it as a toolkit for cleaning and preparing your data for analysis.

Why use dplyr?

  • Makes data cleaning code readable and logical

  • Uses the pipe operator (|>, introduced in Coding a Plot) to chain operations together, so that each step starts from the result of the one before

  • Essential for preparing data before creating visualizations or running analyses

Remember: You cannot create meaningful visualizations or run valid statistical analyses without clean data!

13.2 Naming the variables you make here: _#CAT

Every variable you create in this chapter is categorical, and its name says so. In this playbook a variable made by cutting a score into bands or by collapsing a question’s response options ends in _#CAT. CAT says the variable is categorical: its numbers are labels for categories, not amounts, so it is never averaged or plotted as if it were a score. The number in front of CAT is how many categories the variable has, which is the same as how many response options it has after you are done: 3CAT for three (EDUCATION_3CAT, HEALTH_3CAT, POL_3CAT), 4CAT for four (DISTRESS_4CAT), 6CAT for six (RACIALIZED_6CAT, or RACIALIZED_5CAT when the single-choice question gives five).

A binary variable, whether it came from the survey or you made it here, ends in _01 and never in CAT: CAT is reserved for three or more categories. A binary is either coded 0 and 1, or a factor with two levels, and either way the name says what the 1 (the second level) means: DISTRESS_01 (1 = likely distressed), RACIALIZED_01 (1 = racialized as a person of color, 0 = racialized as white), TREATED_01 (1 = has received treatment).

So the ending tells you the number of groups: _01 means two, _3CAT means three, _4CAT means four. A yes/no question that came straight from the survey is already _01 (Name and Recode Variables in Qualtrics); shared variables such as TIME and CONSENT keep their plain names. When you see CAT at the end of a name, it means someone collapsed or cut an existing variable into three or more groups, and the code that made it should be in the report.

The number is not decoration. How many categories a variable has decides which test it goes into: two categories means a two-group comparison or a binary predictor, three or more means a three-plus-group comparison. The Analysis Map sorts the tests by exactly that count, so a name that carries the count tells you, and anyone reading your code, which row of the map you are on before you look at a single value.

GoGo: how long should a variable name be?

Aim for 6 to 10 characters for the measure’s name, and 14 or fewer for the whole tag including the subscale and the item number (AVOID_SLEEP1 is 12). That is short enough to read in a column header, in a plot legend, and in a line of R code, and long enough to be decoded without the codebook.

The tools will let you go longer, so the limit is readability, not software: R accepts names up to 10,000 characters, and Qualtrics only truncates an export tag when you export to SPSS, which cuts at 64. A name that needs 30 characters usually needs an abbreviation instead: Health Information Avoidance becomes AVOID, Kessler Psychological Distress becomes DISTRESS. Put the full name in your team codebook, where the variable name abbreviation gets explained once.

ResourcesResources: what is a .sav file, and why read_sav()?

Play 1 uses a dataset from an earlier FRI team that was saved in SPSS format, .sav. It is the same kind of thing as your team’s .xlsx file, a table with one row per person, in a different file type. The pattern from Import Data Once holds: the import line has the same three parts, and only the function changes with the file type. read_excel() (package readxl) reads .xlsx, read.csv() reads .csv, and read_sav() (package haven) reads .sav. user_na = FALSE tells read_sav() to bring SPSS’s “user missing” codes in as ordinary numbers, so that the next chunk can turn -99 and -50 into NA the same way you do for an Excel file. You will not need read_sav() for your own data unless your team receives an SPSS file.

13.3 📋 The Play

oralhealthdata <- read_sav("data/10.31.2024.oralhealth.sav", user_na = FALSE)
## replace with NA
oralhealthdata <- oralhealthdata |>
  replace_with_na_all(condition = ~.x %in% c(-99, -50))

13.3.1 Play 1: Collapse Seven Education Categories into Three (EDUCATION_3CAT)

When working with survey data, we often need to collapse detailed response categories into broader groups. This is useful for:

  • Simplifying analysis

  • Creating groups with adequate sample sizes

  • Making results easier to interpret

13.3.2 Original Education Variable

Our survey asked about education level with 7 response options:

  1. Less than high school

  2. High school diploma or GED

  3. Some college, but no degree

  4. Associate’s or technical degree

  5. Bachelor’s degree

  6. Master’s degree

  7. Doctoral or professional degree

13.3.3 Recoding into Three Categories

Let’s group these into three meaningful categories:

# Create a new 3-category education variable
oralhealthdata <- oralhealthdata |>
  mutate(
    EDUCATION_3CAT = factor(
      case_when(
        Education_Level %in% c(1, 2, 3, 4) ~ "Less than Bachelor's",
        Education_Level == 5 ~ "Bachelor's Degree",
        Education_Level %in% c(6, 7) ~ "Graduate Degree"
      ),
      levels = c("Less than Bachelor's", "Bachelor's Degree", "Graduate Degree")
    )
  )

# Check the recoding with a cross-tabulation
table(oralhealthdata$Education_Level, oralhealthdata$EDUCATION_3CAT, useNA = "ifany")
   
    Less than Bachelor's Bachelor's Degree Graduate Degree
  1                   11                 0               0
  2                   17                 0               0
  3                    8                 0               0

13.3.4 Play 2: Cut a Score into Categories (the K10)

Sometimes a published scale comes with cut-off scores, and a reader wants to know how many people fall in each band, not only the average. The lab dataset’s DISTRESS1 to DISTRESS10 are the ten items of the Kessler Psychological Distress Scale (K10; Kessler et al., 2002). The K10 is scored by adding the ten items, for a total from 10 to 50 (K10_TOTAL in lab_prep.R). Andrews and Slade (2001) proposed the cut-offs that most surveys use, including the Victorian Population Health Survey (2001):

K10 total Likelihood of a mental disorder
10 to 19 likely to be well
20 to 24 likely to have a mild disorder
25 to 29 likely to have a moderate disorder
30 to 50 likely to have a severe disorder

These are screening bands, not diagnoses: a score says how likely it is that someone would meet criteria in an interview, not that they do.

ImportantImportant: where mh_clean comes from

You do not have an object called mh_clean yet. In Import Data Once you made mh_raw, and that is all. mh_clean is created by lab_prep.R, the script in the class Google Drive (next to the lab data file). Run source("lab_prep.R") (the chunk below) and mh_clean appears in your environment tab. Every Play and Lab from here to the end of the playbook starts with that one line, so if a chunk says object 'mh_clean' not found, that is the line you skipped.

Plays 2 and 3 use the lab dataset, so this is the first time you run lab_prep.R. source("lab_prep.R") runs every line of that .R file, top to bottom, as if you had typed it: it imports ANTH306_LayConceptionsMH_SYNTHETIC.xlsx as mh_raw, keeps the serious and attentive responses, turns -99 and -50 into NA, reverses the _R items, and averages each multi-item measure into a composite (Creating Composites), all as mh_clean. From here on, every Lab starts with this one line instead of repeating those steps. The file must be in your project folder (next to your .Rproj, not inside data), and your load-library chunk must run first.

source("lab_prep.R")   # creates mh_clean, with K10_TOTAL
Resourceslab_prep.R already has these lines

Open lab_prep.R and you will find DISTRESS_4CAT, DISTRESS_01, and the RACIALIZED variables at the bottom, so that later chapters can use them. Plays 2 and 3 build them again in front of you so that you can see how. Running the code below simply replaces those columns with identical ones.

13.3.5 Four categories: DISTRESS_4CAT

case_when() reads its conditions from the top down and stops at the first one that is true, so each line only has to name its upper limit. factor() with levels = puts the four groups in order, so that tables and plots list them from Well to Severe instead of alphabetically.

mh_clean <- mh_clean |>
  mutate(
    DISTRESS_4CAT = factor(
      case_when(
        K10_TOTAL <= 19 ~ "Well",
        K10_TOTAL <= 24 ~ "Mild",
        K10_TOTAL <= 29 ~ "Moderate",
        K10_TOTAL >= 30 ~ "Severe"
      ),
      levels = c("Well", "Mild", "Moderate", "Severe")
    )
  )

# ALWAYS check a transformation: is every score in the right band?
mh_clean |>
  group_by(DISTRESS_4CAT) |>
  summarise(n = n(), lowest = min(K10_TOTAL), highest = max(K10_TOTAL))
# A tibble: 5 × 4
  DISTRESS_4CAT     n lowest highest
  <fct>         <int>  <dbl>   <dbl>
1 Well             91     12      19
2 Mild             50     20      24
3 Moderate         19     25      29
4 Severe            5     31      43
5 <NA>             23     NA      NA
#source: Andrews & Slade (2001) cut-offs; R4DS Ch. 3 (Wickham et al., 2023) https://r4ds.hadley.nz/data-transform.html
#explanation: K10_TOTAL is cut into the four published bands; the summary table proves that each band's lowest and highest scores fall inside its limits

13.3.6 Two categories: DISTRESS_01

For a comparison of two groups (Compare Groups), or as a predictor in a regression (Relate 3+ Variables), a binary version is often all you need. The K10 cut-off for “likely to have a disorder” is 20 or more, so the line falls between 19 and 20 (a score of 19.5 is impossible, since the total is a whole number). A binary variable is coded 0 and 1 and named for the 1: here, 1 means likely distressed.

mh_clean <- mh_clean |>
  mutate(DISTRESS_01 = if_else(K10_TOTAL >= 20, 1, 0))

mh_clean |> count(DISTRESS_4CAT, DISTRESS_01)    # Well -> 0; Mild, Moderate, Severe -> 1
# A tibble: 5 × 3
  DISTRESS_4CAT DISTRESS_01     n
  <fct>               <dbl> <int>
1 Well                    0    91
2 Mild                    1    50
3 Moderate                1    19
4 Severe                  1     5
5 <NA>                   NA    23
#source: The Quantitative Playbook for R (McCarty, 2026)
#explanation: if_else() gives a 1 to everyone at or above the cut-off and a 0 to everyone below it; the count shows that the binary variable lines up with the four bands

13.3.7 Play 3: Combine Select-All Boxes into One Variable (RACIALIZED)

Teams asked the racial/ethnic identity question in one of two ways (Name and Recode Variables in Qualtrics). In the lab dataset it was select all that apply, so Steps 1 to 3 below combine the boxes; if your team asked it as single choice, skip to the shorter version after Step 3. Here, people could check more than one box, and Qualtrics exported one column per box: RACIALIZED_1 (American Indian or Alaska Native), RACIALIZED_2 (Asian), RACIALIZED_3 (Black or African American), RACIALIZED_4 (Hispanic or Latine), RACIALIZED_5 (Middle Eastern or North African), RACIALIZED_6 (Native Hawaiian or Pacific Islander), RACIALIZED_7 (White), RACIALIZED_8 (Other, with the write-in in RACIALIZED_8_TEXT), and RACIALIZED_99 (Prefer not to say). A cell holds the option’s code when the box was checked and is blank (NA) otherwise. Look at a few rows first:

mh_clean |>
  select(RACIALIZED_1:RACIALIZED_99) |>
  head(8)
# A tibble: 8 × 9
  RACIALIZED_1 RACIALIZED_2 RACIALIZED_3 RACIALIZED_4 RACIALIZED_5 RACIALIZED_6
         <dbl>        <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
1           NA            2           NA           NA           NA           NA
2           NA           NA           NA           NA            5           NA
3           NA           NA           NA            4           NA           NA
4           NA           NA           NA            4           NA           NA
5           NA           NA           NA           NA           NA           NA
6           NA            2           NA           NA           NA           NA
7           NA           NA           NA           NA           NA           NA
8           NA           NA           NA            4           NA           NA
# ℹ 3 more variables: RACIALIZED_7 <dbl>, RACIALIZED_8 <dbl>,
#   RACIALIZED_99 <dbl>

Nine columns cannot go into a table or a model as they are. The plan: count the boxes each person checked, then use case_when() to sort people into groups, putting anyone who checked two or more boxes into their own group instead of forcing them into one.

13.3.8 Step 1: count the checked boxes

!is.na() turns each cell into TRUE (checked) or FALSE (blank), and rowSums() adds up the TRUEs across the eight identity columns. RACIALIZED_99 is left out: declining to answer is not an identity.

mh_clean <- mh_clean |>
  mutate(RACIALIZED_N = rowSums(!is.na(across(RACIALIZED_1:RACIALIZED_8))))

mh_clean |> count(RACIALIZED_N)
# A tibble: 3 × 2
  RACIALIZED_N     n
         <dbl> <int>
1            0     3
2            1   166
3            2    19

13.3.9 Step 2: sort people into groups: RACIALIZED_6CAT

The first condition that is true wins, so “two or more” has to come first; otherwise a person who checked Asian and White would land in whichever single-box line came first. Groups with very few people (codes 1, 5, 6, and 8) are combined into “Another identity”, because a group of three cannot support a comparison. factor() with levels = puts White first, which makes it the comparison group in every model later.

mh_clean <- mh_clean |>
  mutate(
    RACIALIZED_6CAT = factor(
      case_when(
        RACIALIZED_N >= 2     ~ "Two or more",
        !is.na(RACIALIZED_7)  ~ "White",
        !is.na(RACIALIZED_2)  ~ "Asian",
        !is.na(RACIALIZED_3)  ~ "Black",
        !is.na(RACIALIZED_4)  ~ "Hispanic or Latine",
        RACIALIZED_N == 1     ~ "Another identity"      # codes 1, 5, 6, 8: too few to stand alone
      ),
      levels = c("White", "Asian", "Black", "Hispanic or Latine", "Another identity", "Two or more")
    )
  )

# ALWAYS check a transformation
mh_clean |> count(RACIALIZED_6CAT)
# A tibble: 7 × 2
  RACIALIZED_6CAT        n
  <fct>              <int>
1 White                 94
2 Asian                 19
3 Black                 31
4 Hispanic or Latine    17
5 Another identity       5
6 Two or more           19
7 <NA>                   3
#source: The Quantitative Playbook for R (McCarty, 2026); R4DS Ch. 3 https://r4ds.hadley.nz/data-transform.html
#explanation: people who checked two or more boxes become one group; single-box people are sorted by which box; the four smallest groups are combined; people who only declined get NA

People who checked only “Prefer not to say” match none of the conditions, so they get NA, which is what you want: a decline is missing, not a group.

13.3.10 Step 3: the binary version: RACIALIZED_01

Many research questions compare people racialized as white with everyone else. Build the 0/1 version from RACIALIZED_6CAT, so the two variables can never disagree.

mh_clean <- mh_clean |>
  mutate(RACIALIZED_01 = case_when(RACIALIZED_6CAT == "White" ~ 0,
                                      !is.na(RACIALIZED_6CAT)    ~ 1))

mh_clean |> count(RACIALIZED_6CAT, RACIALIZED_01)
# A tibble: 7 × 3
  RACIALIZED_6CAT    RACIALIZED_01     n
  <fct>                      <dbl> <int>
1 White                          0    94
2 Asian                          1    19
3 Black                          1    31
4 Hispanic or Latine             1    17
5 Another identity               1     5
6 Two or more                    1    19
7 <NA>                          NA     3
#source: The Quantitative Playbook for R (McCarty, 2026)
#explanation: 0 = racialized as white, 1 = racialized as a person of color (every other group, including Two or more); NA stays NA

13.3.11 The single-choice version: RACIALIZED_5CAT

If your survey asked for one answer, there are no boxes to count: you have one column, RACIALIZED, with the codes 1 to 8 (and NA where -99 was). Sort the codes into groups with case_when() in one step. Nobody can be in two groups, so there is no “Two or more”; that leaves five groups, and the naming rule says the name must say so: RACIALIZED_5CAT, not _6CAT.

cleandata <- cleandata |>
  mutate(
    RACIALIZED_5CAT = factor(
      case_when(
        RACIALIZED == 7              ~ "White",
        RACIALIZED == 2              ~ "Asian",
        RACIALIZED == 3              ~ "Black",
        RACIALIZED == 4              ~ "Hispanic or Latine",
        RACIALIZED %in% c(1, 5, 6, 8) ~ "Another identity"     # too few to stand alone
      ),
      levels = c("White", "Asian", "Black", "Hispanic or Latine", "Another identity")
    ),
    RACIALIZED_01 = case_when(RACIALIZED_5CAT == "White" ~ 0,
                                 !is.na(RACIALIZED_5CAT)    ~ 1)
  )

cleandata |> count(RACIALIZED, RACIALIZED_5CAT, RACIALIZED_01)   # ALWAYS check a transformation

#source: The Quantitative Playbook for R (McCarty, 2026)
#explanation: one code per person becomes one of five groups; the four smallest groups are combined; NA (declined) stays NA; the binary is built from the 5-group version so they can never disagree

Whichever version you have, look at the counts before you decide which groups to combine: “Another identity” exists because groups of three or five people cannot support a comparison, and your team’s numbers may fall differently.

CautionCaution: the name is RACIALIZED, and the binary is a teaching example

The variable is called RACIALIZED, not RACE, because the survey recorded how people are classified within a system of racial sorting, not a trait they carry (Fields & Fields, 2012; Zuberi & Bonilla-Silva, 2008; D’Ignazio & Klein, 2020; see Data Equity). When you report a result, write “people racialized as Black scored …”, not “Black people are …”. And RACIALIZED_01 is a coarse split that hides real differences between the groups it lumps together. It is here so that you can learn how a binary predictor works. A real report should also show RACIALIZED_6CAT (or RACIALIZED_5CAT).

ResourcesTwo lab variables with no CAT, on purpose: MH_TREATED and MH_KNOWS

The lab dataset has two yes/no variables, MH_TREATED (1 = has received counseling, therapy, or medication for a mental health concern, 0 = has not) and MH_KNOWS (1 = knows someone diagnosed with a mental illness, 0 = does not). They came straight from the survey, coded 0 and 1 and named for the 1, so they do not end in CAT: nobody transformed them. If you cut or combine one of them in this chapter, the new variable is the one that gets the ending. Under the naming rules your own survey would call them TREATED_01 and KNOWS_01; the lab file keeps its older names.

ImportantRequired: cite the cut-offs

If you cut a published scale into categories in your report, cite where the cut-offs come from, the way the K10 references below do. Cut-offs you chose yourself are okay if there is existing theory to inform the decision or for practical purposes, but you must report what you did transparently in your methods section and with your R code.

13.4 🏈 The Lab

Ref the raccoon

Ref’s kickoff. The Play showed three moves: collapse categories (education), cut a score into bands (K10), and combine boxes (RACIALIZED). The Lab uses the same moves on four other variables in mh_clean, and later chapters use what you make here: POL_3CAT in Compare Groups and HEALTH_3CAT in Compare 3+ Groups. Try each play before you open my check.

Before you start. Open your lab .qmd, run your load-library chunk (dplyr must be loaded), and run source("lab_prep.R"). That line is what creates mh_clean (you only have mh_raw from Import Data Once); check the environment tab shows it before you go on. After every play, count the new variable against the old one, the way the Play did: a transformation you have not checked is a foul waiting to happen.

13.4.1 Lab Play 1: Collapse categories (EDUCATION_3CAT)

EDUCATION has six levels (1 = Some high school or less … 6 = Graduate or professional degree) and a 7 that means Prefer not to say. Make EDUCATION_3CAT with the same three groups as the Play (less than a bachelor’s, bachelor’s, graduate), in that order. Then count EDUCATION by EDUCATION_3CAT.

  • 90 less than bachelor’s, 63 bachelor’s, 31 graduate, and 4 NA.
  • The NAs are the people who answered 7. No line in your case_when() mentions 7, so they get NA, which is right: Prefer not to say is missing, not a fourth group. (This older survey coded it 7. Your survey codes it -99, which Import Data Once already turned into NA.)
  • In the count, every value of EDUCATION should sit in exactly one group. If 5 appears in two rows, a condition overlaps.
mh_clean <- mh_clean |>
  mutate(EDUCATION_3CAT = factor(
    case_when(
      EDUCATION %in% c(1, 2, 3, 4) ~ "Less than Bachelor's",
      EDUCATION == 5               ~ "Bachelor's Degree",
      EDUCATION == 6               ~ "Graduate Degree"),
    levels = c("Less than Bachelor's", "Bachelor's Degree", "Graduate Degree")))

mh_clean |> count(EDUCATION, EDUCATION_3CAT)    # ALWAYS check a transformation

#source: The Quantitative Playbook for R (McCarty, 2026); R4DS Ch. 3 https://r4ds.hadley.nz/data-transform.html
#explanation: six education levels become three ordered groups; 7 (prefer not to say) matches no condition and becomes NA

13.4.2 Lab Play 2: Collapse and exclude (POL_3CAT)

POLITICALBELIEFS has six labels: 1 = Leftist, 2 = Liberal, 3 = Moderate, 4 = Conservative, 5 = Far right, 6 = Libertarian, plus -99 and -50. Make POL_3CAT with three groups, Left (1 and 2), Moderate (3), and Right (4 and 5), in that order. Libertarians are too few to be a group of their own and do not fit on the left-right line, so leave them out. Then count.

  • 90 Left, 26 Moderate, 47 Right, and 25 NA.
  • The NAs are the Libertarians, the Prefer not to say (-99), and the Don’t know (-50) answers together. You did not have to write a line for any of them: whatever matches no condition becomes NA.
  • Foul to watch for: POLITICALBELIEFS == 1 | 2 looks right but puts everyone in Left. “Is 1 or 2” is %in% c(1, 2).
mh_clean <- mh_clean |>
  mutate(POL_3CAT = factor(
    case_when(
      POLITICALBELIEFS %in% c(1, 2) ~ "Left",
      POLITICALBELIEFS == 3         ~ "Moderate",
      POLITICALBELIEFS %in% c(4, 5) ~ "Right"),
    levels = c("Left", "Moderate", "Right")))

mh_clean |> count(POLITICALBELIEFS, POL_3CAT)

#source: The Quantitative Playbook for R (McCarty, 2026)
#explanation: six political labels become three ordered groups; Libertarian, -99, and -50 match no condition and become NA, so they drop out of any comparison of the three groups

13.4.3 Lab Play 3: Collapse an ordered scale (HEALTH_3CAT)

HEALTHSTATUS runs from 1 = Poor to 5 = Excellent. Make HEALTH_3CAT with three groups: Poor or fair (1 and 2), Good (3), and Very good or excellent (4 and 5), in that order. Compare 3+ Groups uses exactly this variable, so spell the labels the same way.

  • 36 poor or fair, 81 good, 61 very good or excellent, 10 NA.
  • levels(mh_clean$HEALTH_3CAT) lists the three groups from poor to excellent. If they come out alphabetically (Good first), you forgot levels = in factor().
mh_clean <- mh_clean |>
  mutate(HEALTH_3CAT = factor(
    case_when(
      HEALTHSTATUS %in% c(1, 2) ~ "Poor or fair",
      HEALTHSTATUS == 3         ~ "Good",
      HEALTHSTATUS %in% c(4, 5) ~ "Very good or excellent"),
    levels = c("Poor or fair", "Good", "Very good or excellent")))

mh_clean |> count(HEALTHSTATUS, HEALTH_3CAT)
levels(mh_clean$HEALTH_3CAT)

#source: The Quantitative Playbook for R (McCarty, 2026)
#explanation: the five-point self-rated health item becomes three ordered groups, with the ends combined so that each group has enough people

13.4.4 Lab Play 4: Cut a score into a binary variable (SES_01)

SOCIALSTATUS is the MacArthur ladder, 1 (bottom) to 10 (top). Its codebook row says to dichotomize at the top: 8 to 10 = higher subjective status. Make SES_01, coded 0 and 1 and named for the 1, with if_else(), the way the Play made DISTRESS_01. Then count SOCIALSTATUS by SES_01.

  • 41 people get a 1 (rungs 8, 9, 10) and 145 get a 0 (rungs 1 to 7); 2 are NA because they skipped the ladder.
  • In the count, every rung from 1 to 7 sits next to a 0 and every rung from 8 to 10 next to a 1. If rung 8 sits next to a 0, you wrote > 8 when you meant >= 8.
  • This cut-off comes from the codebook, not from a published paper, so your Methods sentence would say so: “subjective social status was dichotomized at rungs 8 to 10 for comparison with the rest of the ladder.”
mh_clean <- mh_clean |>
  mutate(SES_01 = if_else(SOCIALSTATUS >= 8, 1, 0))

mh_clean |> count(SOCIALSTATUS, SES_01)

#source: The Quantitative Playbook for R (McCarty, 2026)
#explanation: if_else() gives a 1 to rungs 8 to 10 and a 0 to everyone else; NA stays NA

Ref the raccoon blowing his whistle

Ref’s final whistle. Four new variables, four counts that prove they are right. Paste the four mutate() calls into the bottom of your lab_prep.R, under the RACIALIZED lines, so that source("lab_prep.R") makes them every time. Save, render, back up to your ELN.

13.5 🏆 Your Turn

ResourcesThere is no Ref. It’s game time, your turn!

Every categorical variable you use in your report is made here, added to cleandata (never to alldata), with a name that ends in _#CAT or _01CAT. Change every word that starts with SWAP, count every transformation, and use the checklist.

library(dplyr)

# 1. collapse categories: several codes -> fewer groups, in a sensible order
cleandata <- cleandata |>
  mutate(SWAPMEASURE_3CAT = factor(                          # SWAP: the name (number of groups + CAT)
    case_when(
      SWAPOLDVARIABLE %in% c(1, 2) ~ "SWAPLABEL1",            # SWAP: the variable, codes, and labels
      SWAPOLDVARIABLE == 3         ~ "SWAPLABEL2",
      SWAPOLDVARIABLE %in% c(4, 5) ~ "SWAPLABEL3"),
    levels = c("SWAPLABEL1", "SWAPLABEL2", "SWAPLABEL3")))    # SWAP: the order you want in tables and plots
cleandata |> count(SWAPOLDVARIABLE, SWAPMEASURE_3CAT)            # ALWAYS check

# 2. cut a score at a published cut-off: 0/1, named for the 1
cleandata <- cleandata |>
  mutate(SWAPMEASURE_01 = if_else(SWAPSCORE >= 20, 1, 0))  # SWAP: the score and the cut-off; cite it
cleandata |> count(SWAPMEASURE_01)

# 3. RACIALIZED: single choice -> copy the RACIALIZED_5CAT chunk from the Play;
#    select all   -> copy the three steps (RACIALIZED_N, _6CAT, _01CAT) and change nothing but the group names if your counts differ
Criteria Ask yourself
Name Does the new name end in _#CAT (with the number of groups) or _01CAT, in ALL CAPS, 14 characters or fewer?
Every code accounted for Does count(OLD, NEW) show every old code in exactly one group, and only the codes you meant to drop as NA?
Order Did you set levels = so that groups appear in a sensible order (not alphabetical)?
Binary Is the 0/1 variable named for the 1, and does the 1 mean what the name says?
Cut-offs cited If you cut a published scale, is the source in your Methods (the Required box above)? If you chose the cut-off, did you say why?
Group size Does every group have enough people to compare (roughly 20 or more)? If not, combine groups and rename with the new number.
In your report code Are these mutate() lines in the .qmd you will submit, so a reader can rebuild every category?

13.6 References

D’Ignazio, C., & Klein, L. F. (2020). Data feminism. MIT Press. https://data-feminism.mitpress.mit.edu/

Fields, K. E., & Fields, B. J. (2012). Racecraft: The soul of inequality in American life. Verso.

Zuberi, T., & Bonilla-Silva, E. (Eds.). (2008). White logic, white methods: Racism and methodology. Rowman & Littlefield.

Andrews, G., & Slade, T. (2001). Interpreting scores on the Kessler Psychological Distress Scale (K10). Australian and New Zealand Journal of Public Health, 25(6), 494–497. https://doi.org/10.1111/j.1467-842X.2001.tb00310.x

Kessler, R. C., Andrews, G., Colpe, L. J., Hiripi, E., Mroczek, D. K., Normand, S.-L. T., Walters, E. E., & Zaslavsky, A. M. (2002). Short screening scales to monitor population prevalences and trends in non-specific psychological distress. Psychological Medicine, 32(6), 959–976. https://doi.org/10.1017/S0033291702006074

Kessler, R. C., Barker, P. R., Colpe, L. J., Epstein, J. F., Gfroerer, J. C., Hiripi, E., Howes, M. J., Normand, S.-L. T., Manderscheid, R. W., Walters, E. E., & Zaslavsky, A. M. (2003). Screening for serious mental illness in the general population. Archives of General Psychiatry, 60(2), 184–189. https://doi.org/10.1001/archpsyc.60.2.184

Victorian Department of Human Services. (2001). Victorian Population Health Survey 2001: Selected findings. Melbourne: Department of Human Services.

Save → Render → Back up to ELN → Quit, Don’t Save workspace. Details: Ref’s Quick Checklist.