5 · Analytics & data science

15. R for Statistics and Analysis

The tidyverse, data frames, statistical modelling and R Markdown reporting.

9 min read · 3 MCQs

Why R persists

R was built by statisticians for statistics. Its modelling functions, diagnostics and visualisation grammar remain first class, and it dominates in biostatistics, epidemiology, econometrics and academic research.

Tidyverse idioms

dplyr verbs — filter, select, mutate, group_by, summarise — compose through the pipe into readable transformation chains. tidyr reshapes between wide and long; ggplot2 builds plots by layering a grammar of graphics onto mapped aesthetics.

library(dplyr); library(ggplot2)

daily <- orders |>
  filter(status == "paid") |>
  mutate(day = as.Date(created_at)) |>
  group_by(day, country) |>
  summarise(revenue = sum(total_cents) / 100,
            buyers  = n_distinct(customer_id), .groups = "drop")

ggplot(daily, aes(day, revenue, colour = country)) +
  geom_line() +
  labs(title = "Daily revenue by country", y = "Revenue")

Modelling and reporting

lm() and glm() fit linear and generalised linear models with rich summary and diagnostic output. R Markdown and Quarto weave code, results and prose into a reproducible report — the natural artefact when the deliverable is an argument rather than a service.

Chapter quiz

3 questions · pass mark 75%
  1. 1. ggplot2 is based on…

  2. 2. dplyr's summarise is typically preceded by…

  3. 3. R is especially entrenched in…

Answer every question to submit. Progress for da-15 is saved in this browser.