across: Apply a function across a selection of columns

View source: R/across.R

acrossR Documentation

Apply a function across a selection of columns

Description

Apply a function across a selection of columns. For use in arrange(), mutate(), and summarize().

Usage

across(.cols = everything(), .fns = NULL, ..., .names = NULL)

Arguments

.cols

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

.fns

Function to apply. Can be a purrr-style lambda. Can pass also list of functions.

...

Other arguments for the passed function

.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

df <- data.table(
  x = rep(1, 3),
  y = rep(2, 3),
  z = c("a", "a", "b")
)

df %>%
  mutate(across(c(x, y), ~ .x * 2))

df %>%
  summarize(across(where(is.numeric), ~ mean(.x)),
            .by = z)

df %>%
  arrange(across(c(y, z)))

tidytable documentation built on Sept. 11, 2024, 8:05 p.m.