Skip to course content
Free R statistics course

Statistical Data Analytics with R

Unit 05.06: Turn results into data

Twenty tests should produce one table, not twenty printouts to read numbers off.

Turn results into data

broom::tidy() converts a test object into a one-row data frame with the estimate, the interval bounds, the statistic and the p-value as columns. Once results are data, they can be bound together, sorted, filtered, rounded and written out like anything else.

The practical gain is that no number is ever retyped. Transcription errors disappear, and the results table regenerates when the data changes.

One thing to check every time: the sign convention. Which group is subtracted from which depends on factor level order, and getting it backwards inverts every conclusion in the table.

This block tests three metrics and assembles one results table.

suppressPackageStartupMessages({library(broom); library(dplyr)})

set.seed(207)
d <- data.frame(
  metric = rep(c("speed", "accuracy", "satisfaction"), each = 60),
  group  = rep(rep(c("control", "treated"), each = 30), 3),
  value  = c(rnorm(30, 40, 6),  rnorm(30, 44, 6),
             rnorm(30, 0.82, 0.09), rnorm(30, 0.83, 0.09),
             rnorm(30, 6.5, 1.4), rnorm(30, 7.4, 1.4))
)

results <- d |>
  group_split(metric) |>
  lapply(function(x) tidy(t.test(value ~ group, data = x)) |>
                       mutate(metric = x$metric[1])) |>
  bind_rows() |>
  select(metric, estimate, conf.low, conf.high, p.value) |>
  mutate(across(where(is.numeric), \(v) signif(v, 3)))

print(results)

cat("\nEstimates are control minus treated, so a negative value means the\n")
cat("treated group scored higher.\n")
cat("Intervals excluding zero:", sum(results$conf.low * results$conf.high > 0),
    "of", nrow(results), "\n")
cat("Three tests, one data frame, no number retyped.\n")

Three tests become three rows. The estimates are control minus treated, so the negative values mean the treated group scored higher: speed by 4.26 and satisfaction by 1.03, both with intervals excluding zero, while accuracy's interval spans −0.0506 to 0.0311 and concludes nothing. 2 of 3 intervals exclude zero, and no number was retyped to find that out.

The mistake this prevents

The mistake is not checking which direction the estimate runs. A tidied table looks authoritative, and a sign error in it will propagate into every sentence of the report.

Takeaway

Tidy every test into a results table and state the sign convention explicitly in the column name or the caption. Keep all planned comparisons in the table.