12: Data Types Grab Bag
Content for Wednesday, May 6, 2026
Before class
📖 Readings:
During class
We’ll cover:
- Logical vectors —
TRUE/FALSEand how to use them - Comparisons and Boolean operations (
&,|,!) if_else()andcase_when()for conditional values- Numbers — integer vs. double, rounding, summarizing
- Handling
NAin calculations withna.rm = TRUE - Practical application: computing scale scores in psychology data
Slides
View slides in new tab Download PDFEmbedded slides
After class
✅ Practice:
- Use
if_else()to create a new column that labels participants as “young” (under 25) or “older” (25+) - Use
case_when()to recode a Likert scale (1 = “Strongly Disagree”, 2 = “Disagree”, etc.) - Calculate scale scores by averaging across items — remember
na.rm = TRUE - Use
any()andall()to check if any/all values in a column meet a condition - Practice rounding with
round(),floor(), andceiling()— 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
)
)