across {dplyr} | R Documentation |
across()
makes it easy to apply the same transformation to multiple
columns, allowing you to use select()
semantics inside in "data-masking"
functions like summarise()
and mutate()
. See vignette("colwise")
for
more details.
if_any()
and if_all()
are used with to apply the same
predicate function to a selection of columns and combine the
results into a single logical vector.
across()
supersedes the family of "scoped variants" like
summarise_at()
, summarise_if()
, and summarise_all()
.
across(.cols = everything(), .fns = NULL, ..., .names = NULL) if_any(.cols, .fns = NULL, ..., .names = NULL) if_all(.cols, .fns = NULL, ..., .names = NULL)
.fns |
Functions to apply to each of the selected columns. Possible values are:
Within these functions you can use |
... |
Additional arguments for the function calls in |
.names |
A glue specification that describes how to name the output
columns. This can use |
cols, .cols |
< |
across()
returns a tibble with one column for each column in .cols
and each function in .fns
.
if_any()
and if_all()
return a logical vector.
c_across()
for a function that returns a vector
# across() ----------------------------------------------------------------- # Different ways to select the same set of columns # See <https://tidyselect.r-lib.org/articles/syntax.html> for details iris %>% as_tibble() %>% mutate(across(c(Sepal.Length, Sepal.Width), round)) iris %>% as_tibble() %>% mutate(across(c(1, 2), round)) iris %>% as_tibble() %>% mutate(across(1:Sepal.Width, round)) iris %>% as_tibble() %>% mutate(across(where(is.double) & !c(Petal.Length, Petal.Width), round)) # A purrr-style formula iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), ~mean(.x, na.rm = TRUE))) # A named list of functions iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd))) # Use the .names argument to control the output names iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), mean, .names = "mean_{.col}")) iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd), .names = "{.col}.{.fn}")) # When the list is not named, .fn is replaced by the function's position iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), list(mean, sd), .names = "{.col}.fn{.fn}")) # if_any() and if_all() ---------------------------------------------------- iris %>% filter(if_any(ends_with("Width"), ~ . > 4)) iris %>% filter(if_all(ends_with("Width"), ~ . > 2))