| across | 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().
if_any() and if_all() are used 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 dplyr "scoped variants" like summarise_at(), summarise_if(), and
summarise_all() and therefore these functions will not be implemented in poorman.
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 data.frame with one column for each column in .cols and each function in .fns.
if_any() and if_all() return a logical vector.
# across() -----------------------------------------------------------------
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), mean))
iris %>%
mutate(across(where(is.factor), as.character))
# Additional parameters can be passed to functions
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), mean, 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 = c("mean_sepal_length", "mean_sepal_width"))
)
# if_any() and if_all() ----------------------------------------------------
iris %>%
filter(if_any(ends_with("Width"), ~ . > 4))
iris %>%
filter(if_all(ends_with("Width"), ~ . > 2))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.