Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

summarize_across..html

Summarize multiple columns


Description

Summarize multiple columns simultaneously

Usage

summarize_across.(
  .df,
  .cols = everything(),
  .fns = NULL,
  ...,
  .by = NULL,
  .names = NULL
)

summarise_across.(
  .df,
  .cols = everything(),
  .fns = NULL,
  ...,
  .by = NULL,
  .names = NULL
)

Arguments

.df

A data.frame or data.table

.cols

vector c() of unquoted column names. tidyselect compatible.

.fns

Functions to pass. Can pass a list of functions.

...

Other arguments for the passed function

.by

Columns to group by

.names

A glue specification that helps with renaming output columns. {.col} stands for the selected column, and {.fn} stands for the name of the function being applied. The default (NULL) is equivalent to "{.col}" for a single function case and "{.col}_{.fn}" when a list is used for .fns.

Examples

test_df <- data.table(
  a = 1:3,
  b = 4:6,
  z = c("a", "a", "b")
)

# Pass a single function
test_df %>%
  summarize_across.(c(a, b), mean, na.rm = TRUE)

# Single function using purrr style interface
test_df %>%
  summarize_across.(c(a, b), ~ mean(.x, na.rm = TRUE))

# Passing a list of functions (with .by)
test_df %>%
  summarize_across.(c(a, b), list(mean, max), .by = z)

# Passing a named list of functions (with .by)
test_df %>%
  summarize_across.(c(a, b),
                    list(avg = mean,
                         max = ~ max(.x)),
                    .by = z)

# Use the `.names` argument for more naming control
test_df %>%
  summarize_across.(c(a, b),
                    list(avg = mean,
                         max = ~ max(.x)),
                    .by = z,
                    .names = "{.col}_test_{.fn}")

tidytable

Tidy Interface to 'data.table'

v0.6.1
MIT + file LICENSE
Authors
Mark Fairbanks [aut, cre], Abdessabour Moutik [ctb], Matt Carlson [ctb], Ivan Leung [ctb], Ross Kennedy [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.