Reshaping Practice: Cognitive Task Performance

A note on AI tools: You may use AI assistants (ChatGPT, Claude, etc.) to help debug your R code — that’s a realistic professional skill. You may not use AI to generate your interpretations. Every write-up sentence must reflect your own reading of the output. If you use AI for any part of this assignment, note where at the end.

The dataset

A simulated within-subjects study measures 30 participants completing three kinds of tasks (a puzzle, a reading task, and math problems), each performed under three background-noise conditions (silence, white noise, music). This is teaching data (simulated for this course, units not specified) — practice the reshaping mechanics, don’t treat the scores as a real finding.

data <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/cognitive_experiment_data.csv")

Part 1: Inspect the Wide Data

str(data)
names(data)
  1. How many rows and columns does the wide data have? Each row is one what? Each of the 9 non-ID columns represents one what?
  2. This is a within-subjects design with two repeated factors (task and noise condition) crossed together. If you reshaped this to long format — one row per participant per task-by-noise combination — how many rows would you expect? Show your arithmetic before running any code.

Part 2: Reshape to Long Format

The column names follow a Task_Condition pattern, but watch out: one task name (Math_Problems) itself contains an underscore, so a naive split on the first underscore would break it. You need a pattern that correctly separates the task from the noise condition regardless of how many underscores the task name has.

library(tidyverse)

long_df <- pivot_longer(
  data,
  cols = -Participant,
  names_to = c("task", "noise"),
  names_pattern = "(.*)_(Silence|White_Noise|Music)"
)

str(long_df)
nrow(long_df)
table(long_df$task, long_df$noise)
  1. Does the row count match your prediction from Question 2? If not, figure out why before continuing.
  2. Look at the table() output. Is the design balanced (same number of observations in every task × noise cell)?

Part 3: Diagnose a Broken Reshape

Now deliberately do it the naive way, to see what goes wrong:

broken <- pivot_longer(
  data,
  cols = -Participant,
  names_to = "condition",
  values_to = "score"
)
str(broken)
length(unique(broken$condition))
  1. How many unique values does condition have in the broken version, and what’s actually in it? Why doesn’t this give you a usable task variable and a usable noise variable the way Part 2 did?
  2. This is exactly the “wrong number of columns after pivot_longer” failure mode from the chapter’s own common-errors table. In your own words, what should you check immediately after any pivot, before doing anything else with the result?

Part 4: A Quick Visual Check

library(ggplot2)
ggplot(long_df, aes(x = noise, y = value, fill = task)) +
  geom_boxplot() +
  labs(x = "Noise condition", y = "Score", fill = "Task")
  1. Does the pattern look consistent across tasks (e.g., does noise affect all three tasks the same way), or does it look like the effect of noise might depend on which task it is? (You’re not running a test here — just describing what the plot suggests.)

Part 5: Looking Ahead

  1. This reshaped long-format data has exactly the structure a two-way repeated-measures ANOVA would need — two within-subject factors (task and noise) crossed on the same 30 people, with Participant as the ID. Without running it, write the aov_ez() call (from Ch 19) you would use to test whether task, noise, and their interaction affect performance.
  2. What is one variable, not captured in this dataset, that could plausibly affect how someone performs across these tasks and noise conditions (a potential confound if you wanted to generalize beyond this specific sample)?