Day 6

in-class
Author

Josef Fruehwald

Published

September 16, 2024

tibble(
  group = c("a", "b", "c"),
  n = 1:3
) |> 
  ggplot(
    aes(
      x = group,
      y = n
    )
  )+
  geom_col(
    aes(
      fill = group
    ),
    color = "black"
  )+
  scale_fill_brewer(palette = "Dark2")

Data operations

filter

babynames |> 
  filter(
    name == "Josef"
  )
babynames |> 
  summarise(
    total = sum(n)
  )

mutate

babynames |> 
  mutate(
    new1 = "I'm new",
    len = nchar(name)
  )

combo

babynames |> 
  filter(name == "Josef") |> 
  summarise(
    total = sum(n)
  )

Split-Apply-Combine

babynames |> 
  filter(
    year == 1880
  ) |> 
  summarise(
    total = sum(n)
  )

.by or group_by()

babynames |> 
  summarise(
    .by = year,
    total = sum(n),
    most = max(n),
    most_name = name[n == max(n)]
  ) 
babynames |> 
  group_by(year) |> 
  summarise(
    total = sum(n)
  )
`filter()`

Be sure to load the tidyverse before using filter() !

babynames |> 
  mutate(nchar = nchar(name)) |> 
  summarise(
    .by = c(year, sex),
    total = sum(n),
    nchar_w = sum(nchar * n)/total
  ) |> 
  ggplot(
    aes(year, nchar_w)
  )+
    geom_point()

tibble(
  day = c(
    "Monday", "Tuesday",
    "Monday", "Tuesday", "Wednesday"
  ),
  name = c(
    "Oakley", "Oakley",
    "Skyler", "Skyler", "Skyler"
  ),
  lines = c(
    10, 30,
     5,  5, 10
  )
)->
  collab_df

collab_df
tibble(
  year = c(2000, 1995),
  name = c("Oakley", "Skyler")
)->yob

yob
collab_df |> 
  left_join(yob) ->
  collab_df
babynames |> 
  summarise(
    .by = c(year, name),
    total = sum(n)
  ) ->
  total_babies

total_babies
collab_df |> 
  left_join(total_babies)
data("pb52", package = "phonTools")
pb52 |> 
  summarise(
    .by = c(type, vowel),
    across(f0:f3, mean)
  )->
  vowel_means

vowel_means
vowel_means |> 
  ggplot(
    aes(-f2, -f1)
  )+
    geom_text(
      aes(label = vowel)
    )+
    facet_wrap(~type)

tibble(
  month=month.name
) |> 
  mutate(
    oysters = case_when(
      str_detect(month, "r$") ~ "ok",
      .default = "no"
    )
  )

Reuse

CC-BY 4.0

Citation

BibTeX citation:
@online{fruehwald2024,
  author = {Fruehwald, Josef},
  title = {Day 6},
  date = {2024-09-16},
  url = {https://lin611-2024.github.io/notes/in-class/2024-09-16_day6.html},
  langid = {en}
}
For attribution, please cite this work as:
Fruehwald, Josef. 2024. “Day 6.” September 16, 2024. https://lin611-2024.github.io/notes/in-class/2024-09-16_day6.html.