diff --git a/R/across.R b/R/across.R index 4c01413a46..ad9bd54b75 100644 --- a/R/across.R +++ b/R/across.R @@ -108,6 +108,17 @@ #' iris %>% #' mutate(across(where(is.double) & !c(Petal.Length, Petal.Width), round)) #' +#' # Using an external vector of names +#' cols <- c("Sepal.Length", "Petal.Width") +#' iris %>% +#' mutate(across(all_of(cols), round)) +#' +#' # If the external vector is named, the output columns will be named according +#' # to those names +#' names(cols) <- tolower(cols) +#' iris %>% +#' mutate(across(all_of(cols), round)) +#' #' # A purrr-style formula #' iris %>% #' group_by(Species) %>% @@ -126,6 +137,12 @@ #' group_by(Species) %>% #' summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd), .names = "{.col}.{.fn}")) #' +#' # If a named external vector is used for column selection, .names will use +#' # those names when constructing the output names +#' iris %>% +#' group_by(Species) %>% +#' summarise(across(all_of(cols), mean, .names = "mean_{.col}")) +#' #' # When the list is not named, .fn is replaced by the function's position #' iris %>% #' group_by(Species) %>% diff --git a/man/across.Rd b/man/across.Rd index a95894fd35..7907d0a9dd 100644 --- a/man/across.Rd +++ b/man/across.Rd @@ -152,6 +152,17 @@ iris \%>\% iris \%>\% mutate(across(where(is.double) & !c(Petal.Length, Petal.Width), round)) +# Using an external vector of names +cols <- c("Sepal.Length", "Petal.Width") +iris \%>\% + mutate(across(all_of(cols), round)) + +# If the external vector is named, the output columns will be named according +# to those names +names(cols) <- tolower(cols) +iris \%>\% + mutate(across(all_of(cols), round)) + # A purrr-style formula iris \%>\% group_by(Species) \%>\% @@ -170,6 +181,12 @@ iris \%>\% group_by(Species) \%>\% summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd), .names = "{.col}.{.fn}")) +# If a named external vector is used for column selection, .names will use +# those names when constructing the output names +iris \%>\% + group_by(Species) \%>\% + summarise(across(all_of(cols), mean, .names = "mean_{.col}")) + # When the list is not named, .fn is replaced by the function's position iris \%>\% group_by(Species) \%>\%