OPEN EDUCATIONAL RESOURCE · INSTRUCTOR RESOURCES

Lesson 0 · Public Instructor Solutions

Original worked examples, R script, expected results and interpretation guidance for the programming foundations lesson.

Updated: 5 November 2026 · Independent ferdo Lab material; not an official UCL syllabus or exam

This answer key is public

All exercises and numbers below are original, fictional teaching examples. They are not solutions to UCL's original assessments. Publishing the key is intentional; students should attempt the starter file before checking it.

Lesson 0 explanation · Student starter .R · Teaching plan

Worked R program

# ferdo R Lab — Lesson 0 · instructor worked solution (synthetic data)
scores <- c(12, 10, 14, NA, 9)
length(scores)                 # 5
mean(scores)                  # NA
mean(scores, na.rm = TRUE)    # 11.25
sum(!is.na(scores))           # 4

id <- c("A", "B", "C", "D", "E")
before <- c(12, 10, 14, NA, 9)
after  <- c(14, 13, 13, 15, 12)
delta <- after - before
print(data.frame(id, before, after, delta))
valid_pairs <- sum(!is.na(delta))
increased <- sum(delta > 0, na.rm = TRUE)
decreased <- sum(delta < 0, na.rm = TRUE)
paired_mean_change <- mean(delta, na.rm = TRUE)
pre_mean <- mean(before, na.rm = TRUE)
post_mean <- mean(after)
stopifnot(valid_pairs == 4, increased == 3, decreased == 1,
          paired_mean_change == 1.75,
          pre_mean == 11.25, post_mean == 13.4)

Download instructor .R solution

Uses only base R; no additional packages or private data.

Expected numerical answers

CalculationAnswerReason
Number of values5`length()` counts the missing element too.
Mean without removing NANAR propagates missingness under the default rule.
Mean of observed pretest values11.25(12+10+14+9)/4
Valid matched participants4D has no pretest; D's change is unknown.
Individual change values2, 3, −1, NA, 3Computed as posttest minus pretest for each person.
Positive / negative changes3 / 1Only matched records can be classified.
Mean of matched changes1.75(2+3−1+3)/4
Mean of all posttest values13.40(14+13+13+15+12)/5
Posttest mean − observed pretest mean2.15Different denominators; not the same as mean matched change.

Six exercises with model answers

A1 — Describe an algorithm

Specify input and desired output → identify missing answers → select valid answers → compute sum and sample size → divide sum by valid count → document the exclusion rule.

A2 — Predict an output

`mean(c(2,4,6))` = 4.

B1 — Count valid pretest records

`sum(!is.na(before))` = 4.

B2 — Determine change direction

`sum(delta>0,na.rm=TRUE)` = 3; `sum(delta<0,na.rm=TRUE)` = 1.

C1 — Fix a missing mean

`mean(before)` returns NA; `mean(before, na.rm=TRUE)` returns 11.25. This excludes missingness from the calculation but does not solve attrition bias.

C2 — Challenge causality

A difference alone cannot establish a treatment effect: consider missing observations, regression to the mean, selection and control-group/counterfactual evidence.

Why 1.75 ≠ 2.15

The mean of paired individual changes uses four people because D has no pretest; the mean of all posttest scores includes five people. Subtracting the mean of four observed pretests from the mean of five posttests compares different sets of records. Even correct arithmetic can be a poor match for the research question.

Neither figure, by itself, establishes causation.

Common conceptual mistakes

Exit-ticket model answers

  1. Programming specifies a testable sequence to solve a problem; typing syntax is only one part.
  2. No. R executes code; RStudio makes writing and running it easier.
  3. Define the question, input and desired output, then plan the transformations and checks.
  4. Check whether values are truly missing and choose a defensible rule for handling NA.
  5. An error-free command can use incorrect labels, omit key observations or support an invalid interpretation.

Back to open instructor desk · 90-minute teaching sequence