# HSCI 410 Lesson 1, Activity 1.3: verifying, editing and documenting bp01.csv
# Run-along script for the narrated walkthrough. It reads the course data
# directly from the course website, so there is nothing to set up: open it
# in RStudio and run each part with Ctrl+Enter when the narration asks.

# 1. Read the raw file
library(dplyr)                    # part of the tidyverse
bp_raw <- read.csv("https://heal-learn.ca/r-activities/data/bp01.csv")   # course data, read from the website
nrow(bp_raw)                      # number of rows

# 2. A codebook: what each variable should contain
codebook <- tribble(
  ~variable,      ~label,                             ~units_or_codes,
  "id",           "Participant identifier",           "BP0001 to BP1092",
  "age",          "Age",                              "years",
  "sex",          "Sex",                              "Female, Male",
  "smoker",       "Smoking status",                   "No, Yes",
  "bmi",          "Body mass index",                  "kg/m2",
  "systolic",     "Systolic blood pressure",          "mmHg",
  "diastolic",    "Diastolic blood pressure",         "mmHg",
  "on_treatment", "Takes blood-pressure medication",  "No, Yes"
)
codebook

# 3. Continuous variables: range, missing values, extremes, shape
summary(bp_raw[, c("age", "bmi", "systolic", "diastolic")])
colSums(is.na(bp_raw))            # number missing in every variable
head(sort(bp_raw$systolic), 5)    # the five smallest values
tail(sort(bp_raw$systolic), 5)    # the five largest values
head(sort(bp_raw$age), 5)
tail(sort(bp_raw$age), 5)
sum(bp_raw$systolic == 85, na.rm = TRUE)   # how many at the lowest value?
sum(bp_raw$age == 85, na.rm = TRUE)        # how many at the highest age?
hist(bp_raw$systolic, breaks = 30, xlab = "Systolic BP (mmHg)", main = "")

# 4. Categorical variables, consistency and identifiers
table(bp_raw$sex, useNA = "ifany")
table(bp_raw$smoker, useNA = "ifany")
table(bp_raw$on_treatment, useNA = "ifany")
bp_raw |> filter(diastolic > systolic)    # should return no rows
n_distinct(bp_raw$id)                     # compare with nrow()

# 5. Plant three errors on a copy and run the same checks
bp_bad <- bp_raw                       # a copy; bp_raw is untouched
bp_bad$age[c(5, 120, 640)] <- 999      # planted: unknown age typed as 999
bp_bad$sex[c(10, 302)] <- "F"          # planted: abbreviation
bp_bad$sex[450] <- "male"              # planted: lower-case spelling
summary(bp_bad$age)
tail(sort(bp_bad$age), 5)
hist(bp_bad$age, breaks = 30, xlab = "Age (years)", main = "")
table(bp_bad$sex, useNA = "ifany")

# 6. The fix belongs in the cleaning script, never in the raw file
bp_fixed <- bp_bad |>
  mutate(age = na_if(age, 999),
         sex = case_when(sex %in% c("Female", "F") ~ "Female",
                         sex %in% c("Male", "male") ~ "Male"))
