12: Data Types Grab Bag

Content for Wednesday, May 6, 2026

Before class

📖 Readings:

During class

We’ll cover:

  • Logical vectors — TRUE/FALSE and how to use them
  • Comparisons and Boolean operations (&, |, !)
  • if_else() and case_when() for conditional values
  • Numbers — integer vs. double, rounding, summarizing
  • Handling NA in calculations with na.rm = TRUE
  • Practical application: computing scale scores in psychology data

Slides

View slides in new tab Download PDF

Embedded slides

After class

Practice:

  1. Use if_else() to create a new column that labels participants as “young” (under 25) or “older” (25+)
  2. Use case_when() to recode a Likert scale (1 = “Strongly Disagree”, 2 = “Disagree”, etc.)
  3. Calculate scale scores by averaging across items — remember na.rm = TRUE
  4. Use any() and all() to check if any/all values in a column meet a condition
  5. Practice rounding with round(), floor(), and ceiling() — when would you use each?
Notecase_when() is your friend

case_when() is the tidyverse version of “if… else if… else.” It’s especially useful for recoding variables:

data |>
  mutate(
    age_group = case_when(
      age < 18 ~ "Minor",
      age < 25 ~ "Young adult",
      age < 65 ~ "Adult",
      .default = "Senior"
    )
  )

The .default argument catches everything that doesn’t match a previous condition.

Psychology application: scale scores

# Computing a scale score from individual items
survey_data |>
  mutate(
    # Reverse code item 3 (on a 1-5 scale)
    item3_r = 6 - item3,
    # Calculate mean score across items
    scale_score = rowMeans(
      pick(item1, item2, item3_r, item4, item5),
      na.rm = TRUE
    )
  )