library(tidyverse)

library(palmerpenguins)

# filter
penguins |> filter(species == 'Adelie')

# arrange
penguins |> arrange(-island)

penguins |>
  arrange(bill_length_mm / body_mass_g) |>
  mutate(ratio = bill_length_mm / body_mass_g)

penguins |> relocate(year)

penguins |>
  mutate(
    bill_ratio = bill_length_mm / bill_depth_mm,
    .before = 1
  )

# select a range of columns
df <- penguins |>
  mutate(
    b1 = round(bill_length_mm),
    b2 = bill_depth_mm^2,
    b3 = bill_length_mm + bill_depth_mm,
    .before = 1
  )

# select a range of columns
df <- penguins |>
  mutate(
    b1 = round(bill_length_mm),
    b2 = bill_depth_mm^2,
    b3 = bill_length_mm + bill_depth_mm,
    .before = 1
  )

df |> select(num_range("b", c(1, 3))) |> head()


penguins |> select(where(is.numeric)) |> mutate(across(everything(), ~ .x^2))

# 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'
)

# respondent info with labor force status & earnings
atus_resp <- read_csv(
  'http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusresp_2025.dat'
)

atus_dict <- read_csv(
  'http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atus_dict.csv'
)


atus_job |> filter(TRDTOCC1 == 3)

atus_job <- atus_job |> rename("job" = TRDTOCC1)

atus_job <- atus_job |> rename("job" = TRDTOCC1)

# notice the syntax below (==) signs for boolean (yes/no) comparisons.  The `|` symbol means "or" in this context.
atus_filtered <- filter(atus_job, job == 3 | job == 5 | job == 13)

ggplot(atus_filtered, aes(x = job, fill = as_factor(job))) +
  geom_bar() +
  labs(x = 'Job Type', y = 'Count', title = 'Some Job Types in ATUS Data')

# look closer at atus_rost
# demographic
ss <- atus_rost |> select(1, 3, 5)

# data on work
new_ss <- left_join(ss, atus_job, by = 'TUCASEID')
#
ss |> ggplot(aes(x = as_factor(TESEX), y = TEAGE)) + geom_boxplot()

new_ss |> select(1:5, TRTALONE)

# question: in the dataset, who spends
# more time alone, women or men?
