# wk4-monday

# as usual, we will use the tidyverse
library(tidyverse)

# responses concerning personal activity
atus_act <- read_csv(
  'http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusact_2025.dat'
)

# responses concerning demographics
atus_rost <- read_csv(
  'http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusrost_2025.dat'
)

# responses concerning types of jobs
atus_job <- read_csv(
  'http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusresp_2025.dat'
)


# use the dictionary to look up the description of a variable
atus_dict <- read_csv(
    'https://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atus_dict.csv'
  )

atus_dict |>
  filter(name == 'TUSPWK') |>
  select(description)

# this is too long
# use strwrap to wrap the text
atus_dict |>
  filter(name == 'TUSPWK') |>
  select(description) |>
  strwrap(width = 50)

# combine the lines into a single string
atus_dict |>
  filter(name == 'TUSPWK') |>
  select(description) |>
  strwrap(width = 50) |>
  cat(sep = '\n')

# we will want to reuse this code, so let's make a function
get_description <- function(var_name) {
  atus_dict |>
    filter(name %in% var_name) |>
    select(description) |>
    strwrap(width = 50) |>
    cat(sep = '\n')
}

# load the activity lexicon
lex <- read_csv('../data/ATUS/atus_activity_lexicon_2025.csv')

# function to take in a TRCODE and return the description
# this function should be vectorized so it can 
# work within mutate
get_activity_description <- function(trcode) {
  lex |>
    filter(activity_code %in% trcode) |>
    select(description) |> 
    pull()
}

my_id <- sample(atus_act$TUCASEID, 1)
my_act <- atus_act |> filter(TUCASEID == my_id) |> select(TRCODE) |> pull()
filter(lex, activity_code %in% my_act)
filter(atus_rost, TUCASEID == my_id)

old_id <- filter(
  atus_rost, TEAGE >= 65) |> select(TUCASEID) |> pull()

slp <- atus_act |>
  filter(TUCASEID %in% old_id, TRCODE == "010101") |>
  select(TUACTDUR, TUCASEID) |>
  mutate(hour_sleep = TUACTDUR / 60) |>
  filter(hour_sleep < 5)

slp

# determine the unqique pairs of TUCASEID and TRCODE
unique_pairs <- atus_act |>
  select(TUCASEID, TRCODE) |>
  unique()

af <- atus_act |> 
  select(TUCASEID, TRCODE, TUACTIVITY_N, TUSTARTTIM, TUSTOPTIME) |>
  left_join(lex, by = c("TRCODE" = "activity_code"))
  )
