Skip to content

Instantly share code, notes, and snippets.

@HorridTom
Created September 17, 2021 14:50
Show Gist options
  • Save HorridTom/669eb52b06dbd23433aff02dd5711ecb to your computer and use it in GitHub Desktop.
Save HorridTom/669eb52b06dbd23433aff02dd5711ecb to your computer and use it in GitHub Desktop.
Examples of multiple summaries
library(tidyverse)
# Examples of multiple summaries...
# Based on examples here: https://dplyr.tidyverse.org/reference/across.html
# one summary across multiple columns
mtcars %>%
group_by(cyl) %>%
summarise(across(where(is.numeric), mean))
# multiple summaries across multiple columns
mtcars %>%
group_by(cyl) %>%
summarise(across(where(is.numeric),
list(mean = mean, min = min, max = max)))
# with arugments to summary functions
mtcars %>%
group_by(cyl) %>%
summarise(across(where(is.numeric),
list(mean = ~ mean(.x, na.rm = TRUE),
min = ~ min(.x. na.rm = TRUE),
max = ~ max(.x, na.rm = TRUE))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment