# 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))
Run the code above in your browser using DataLab