# HSCI 410 Lesson 1, Activity 1.2: code, enter and version the data
# 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.
# install.packages(c("dplyr", "tidyr", "readr"))   # once per computer

# 1. A missing-value code treated as a real age (Worked Example 1.4)
library(dplyr); library(tidyr); library(readr)
ages <- tibble(id = 1:5, age = c(34, 51, -999, 47, 62))   # id 3: not reported
mean(ages$age)                        # -999 is treated as a real age
ages_fixed <- ages |> mutate(age = na_if(age, -999))      # the code becomes NA
ages_fixed$age
mean(ages_fixed$age)                  # NA: R will not average an unknown value
mean(ages_fixed$age, na.rm = TRUE)    # drop the NA, then average the rest

# 2. Declare the code as missing when a file is read
demo_file <- tempfile(fileext = ".csv")     # a throwaway file for the demo
write_csv(ages, demo_file)
ages_read <- read_csv(demo_file, na = c("", "NA", "-999"),
                      show_col_types = FALSE)
ages_read$age

# 3. Two more faults: text in a numeric field and a compound code
hours <- c("4", "0", "none", "2")     # hours of exercise per week, as entered
mean(as.numeric(hours))               # "none" cannot be read as a number
# smk_drk: first digit smoking (1 no, 2 yes), second digit drinks per week
smk_drk <- c(13, 21, 99, 12)          # 99 = missing
smoker <- smk_drk %/% 10              # split: the first digit
drinks <- smk_drk %% 10               # the second digit
table(smoker)
table(drinks)

# 4. Numeric codes become labelled factors (Worked Example 1.5)
codes <- tibble(id = 1:4, sex = c(1, 2, 2, 1))     # 1 = Female, 2 = Male
codes <- codes |>
  mutate(sex_f = factor(sex, levels = c(1, 2), labels = c("Female", "Male")))
codes
table(codes$sex_f)

# 5. Compare two independent entries (Worked Example 1.6)
entry_a <- tibble(id = 1:5, age = c(34, 51, 29, 47, 62),
                  sbp = c(128, 141, 117, 135, 150))
entry_b <- tibble(id = 1:5, age = c(34, 15, 29, 47, 62),
                  sbp = c(128, 141, 117, 153, 150))
anti_join(entry_a, entry_b, by = c("id", "age", "sbp"))
anti_join(entry_b, entry_a, by = c("id", "age", "sbp"))

# 6. Clean bp01.csv and save the next version (example file log, 5.3)
bp_raw <- read.csv("https://heal-learn.ca/r-activities/data/bp01.csv")   # course data, read from the website
bp_clean <- bp_raw |>
  drop_na(systolic, diastolic, age) |>        # drop the incomplete records
  mutate(
    age_ct   = age - mean(age),                # centred
    age_ctsq = age_ct^2,                       # quadratic term
    age_c3   = cut(age, c(0, 35, 55, Inf),
                   labels = c("young", "middle", "older")),
    htn      = factor(systolic >= 140 | diastolic >= 90,
                      levels = c(FALSE, TRUE),
                      labels = c("normotensive", "hypertensive")))
write_csv(bp_clean, "bp02.csv")       # a new version: bp01 is never changed
cat("bp02.csv", format(Sys.Date()), nrow(bp_clean), "obs,",
    ncol(bp_clean), "vars\n", file = "file_log.txt", append = TRUE)
readLines("file_log.txt")             # the file log so far

# 7. Check the new version
nrow(bp_raw)                          # rows before cleaning
nrow(bp_clean)                        # rows after drop_na()
ncol(bp_clean)                        # 8 original variables + 4 derived
mean(bp_clean$age)                    # the value subtracted to make age_ct
head(bp_clean[, c("id", "age", "age_ct", "age_ctsq", "age_c3")], 3)
count(bp_clean, htn) |> mutate(percent = round(100 * n / sum(n), 1))
