Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Created June 29, 2020 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainfrancois/afebd3dd7aaf861d094e8be6ab18778d to your computer and use it in GitHub Desktop.
Save romainfrancois/afebd3dd7aaf861d094e8be6ab18778d to your computer and use it in GitHub Desktop.
code used in my dplyr talk at e-Rum 2020
library(dplyr, warn.conflicts = FALSE)
library(palmerpenguins)
# multiple rows
penguins %>%
group_by(species) %>%
summarise(
prob = c(.25, .75),
length = quantile(bill_length_mm, prob, na.rm = TRUE),
depth = quantile(bill_depth_mm, prob, na.rm = TRUE)
)
# multiple columns
penguins %>%
group_by(species) %>%
summarise(
broom::tidy(lm(bill_depth_mm ~ bill_length_mm))
)
# working across() columns
penguins %>%
group_by(species) %>%
summarise(
across(starts_with("bill"), min, na.rm = TRUE)
)
# working across() columns
penguins %>%
group_by(species) %>%
summarise(
across(starts_with("bill"),
list(min = min, max = max),
na.rm = TRUE
)
)
# .groups =
penguins %>%
group_by(species, island) %>%
summarise(
prob = c(.25, .75),
length = quantile(bill_length_mm, prob, na.rm = TRUE),
depth = quantile(bill_depth_mm, prob, na.rm = TRUE)
)
penguins %>%
group_by(species, island) %>%
summarise(
length = mean(bill_length_mm, na.rm = TRUE),
depth = mean(bill_depth_mm, na.rm = TRUE)
)
penguins %>%
group_by(species, island) %>%
summarise(
length = mean(bill_length_mm, na.rm = TRUE),
depth = mean(bill_depth_mm, na.rm = TRUE)
)
# .groups = drop
penguins %>%
group_by(species, island) %>%
summarise(.groups = "drop",
length = mean(bill_length_mm, na.rm = TRUE),
depth = mean(bill_depth_mm, na.rm = TRUE)
)
# .groups = keep
penguins %>%
group_by(species, island) %>%
summarise(.groups = "keep",
length = mean(bill_length_mm, na.rm = TRUE),
depth = mean(bill_depth_mm, na.rm = TRUE)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment