across | R Documentation |
Apply a function across a selection of columns. For use in arrange()
,
mutate()
, and summarize()
.
across(.cols = everything(), .fns = NULL, ..., .names = NULL)
.cols |
vector |
.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.
|
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)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.