#Title: DWV Lecture 4 Material
#Author: Jack Reilly
#Purpose: Contains code for lecture 4 slides
#Requires: none
#Output: nothing to disk

#Start with our data
library(tidyverse)

pid<-c(1, 6, 3, 7, 8, 9, 2, 2, 3, 4, 4, 7, 1, 5)
data<-tibble(pid)

#Start with NAs
data$pid_recode <- NA

#Recode PID (see slides)
data$pid_recode[data$pid < 3] <- -1
data$pid_recode[data$pid > 5 & data$pid < 8] <- 1
data$pid_recode[data$pid > 2 & data$pid < 6] <- 0

#Check our work
table(data$pid, data$pid_recode)

#Or in tidyverse: do it all together!
data <- data |>
  mutate(pid_recode2 = case_when(
    pid %in% 1:2 ~ -1,
    pid %in% 3:5 ~ 0,
    pid %in% 6:7 ~ 1,
    pid == 8 | pid == 9     ~ NA
  ))

table(data$pid, data$pid_recode2)

data |> count(pid, pid_recode2)