layout: true


knitr::opts_chunk$set(echo = TRUE, 
                      message = FALSE, 
                      error = FALSE, 
                      warning = FALSE)

# These are the defaults
xaringanExtra::use_extra_styles(
  hover_code_line = TRUE,         #<<
  mute_unhighlighted_code = TRUE  #<<
)

library(knitr)
library(tidyquintro)

class: inverse, middle, center name: pivots

pivots


penguins |> 
  pivot_longer(contains("_")) 

penguins |> 
  pivot_longer(contains("_"), # select the columns
               names_to = c("body_part", "measure", "unit"), # break them into these columns
               names_sep = "_") # break the column names on this character

penguins |> 
  pivot_longer(contains("_"),
               names_to = c("body_part", 
                            "measure", 
                            "unit"),
               names_sep = "_") |> 

  ggplot(aes(x = value, fill = species)) + 
  geom_density() + 
  facet_wrap(~ body_part, scales = "free") +
  scale_fill_viridis_d(alpha = .5) +
  theme(legend.position = "bottom")

penguins_long <- penguins |> 
  pivot_longer(contains("_"),
               names_to = c("body_part", 
                            "measure", 
                            "unit"),
               names_sep = "_") 

penguins_long |> 
  pivot_wider(names_from = c("body_part", "measure", "unit"), # pivot these columns
              values_from = "value", # take the values from here
              names_sep = "_") # separate names_from with this character

class: inverse, middle, center name: summaries

summaries


penguins |> 
  summarise(mean(bill_length_mm, na.rm = TRUE))

penguins |> 
  group_by(species) |> 
  summarise(m_bill_length = mean(bill_length_mm, na.rm = TRUE))

penguins |> 
  group_by(species, island) |> 
  summarise(m_bill_length = mean(bill_length_mm, na.rm = TRUE))

penguins |> 
  group_by(species, island) |> 
  summarise(across(bill_length_mm,
                   mean
  ))

penguins |> 
  group_by(species, island) |> 
  summarise(across(bill_length_mm,
                   list(mean, sd)
  ))

penguins |> 
  group_by(species, island) |> 
  summarise(across(bill_length_mm,
                   list(mean = mean, sd = sd)
  ))

penguins |> 
  group_by(species, island) |> 
  summarise(across(bill_length_mm,
                   list(mean = mean, sd = sd),
                   .names = "{.fn}"
  ))

penguins |> 
  group_by(species, island) |> 
  summarise(across(contains("_"), 
                   list(Mean = mean, 
                        SD = sd,
                        Min = min,
                        Max = max), 
                   na.rm = TRUE))

penguins |> 
  group_by(species, island) |> 
  summarise(across(contains("_"), 
                   list(Mean = mean, 
                        SD = sd,
                        Min = min,
                        Max = max), 
                   na.rm = TRUE,
                   .names = "{.fn}_{.col}"))

penguins |> 
  group_by(species, island) |> 
  summarise(across(contains("_"), 
                   list(Mean = mean, 
                        SD = sd,
                        Min = min,
                        Max = max), 
                   na.rm = TRUE)) |> 
  pivot_longer(contains("_"),
               names_to = c("body_part", "measure", "unit", "stat"),
               names_sep = "_") |> 
  pivot_wider(names_from = stat, values_from = value)

penguins |> 
  # pivot all the columns we want to summarise
  pivot_longer(contains("_"),
               names_to = c("body_part", "measure", "unit", "stat"),
               names_sep = "_",
               values_drop_na = TRUE) |> 
  # Group by wanted grouping variables, including names of columns we made above
  group_by(species, island, body_part, measure, unit) |> 
  # Summarise and give columns just function name
  summarise(across(value, 
                   list(Mean = mean, 
                        SD = sd,
                        Min = min,
                        Max = max),
                   .names = "{.fn}"))

class: inverse, middle, center name: nested

nested data


penguins |> 
    nest_by(species, island)

penguins |> 
    nest_by(species, island) |> 
    mutate(lm_model = list(
      lm(bill_length_mm ~ bill_depth_mm, data = data)
      ))

model_penguins <- penguins |> 
  nest_by(species, island) |> 
  mutate(
    lm_model = list(
      lm(bill_length_mm ~ bill_depth_mm, data = data)
    ),
    table = list(broom::tidy(lm_model))
    )
model_penguins

model_penguins |> 
  unnest(data)

model_penguins |> 
  unnest(table) |> 
  select(-lm_model, -data)


Athanasiamo/tidyquintro documentation built on Oct. 11, 2022, 7:15 p.m.