# The _at() variants directly support strings:
starwars %>%
summarise_at(c("height", "mass"), mean, na.rm = TRUE)
# ->
starwars %>% summarise(across(c("height", "mass"), ~ mean(.x, na.rm = TRUE)))
# You can also supply selection helpers to _at() functions but you have
# to quote them with vars():
starwars %>%
summarise_at(vars(height:mass), mean, na.rm = TRUE)
# ->
starwars %>%
summarise(across(height:mass, ~ mean(.x, na.rm = TRUE)))
# The _if() variants apply a predicate function (a function that
# returns TRUE or FALSE) to determine the relevant subset of
# columns. Here we apply mean() to the numeric columns:
starwars %>%
summarise_if(is.numeric, mean, na.rm = TRUE)
starwars %>%
summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE)))
by_species <- iris %>%
group_by(Species)
# If you want to apply multiple transformations, pass a list of
# functions. When there are multiple functions, they create new
# variables instead of modifying the variables in place:
by_species %>%
summarise_all(list(min, max))
# ->
by_species %>%
summarise(across(everything(), list(min = min, max = max)))
Run the code above in your browser using DataLab