dplyr::across()
across(.cols = everything(), .fns = NULL, ..., .names = NULL) Returns:
tibble · Updated March 13, 2026 · Tidyverse dplyr across where tidyverse data-wrangling
across() and where() are dplyr functions that enable column-wise transformations. across() applies the same function(s) to multiple columns simultaneously, replacing values in place. where() is a selection helper that picks columns based on their type or a condition, making it easy to target specific column types for transformation.
Syntax
across(.cols = everything(), .fns = NULL, ..., .names = NULL)
where(.predicate, ...)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
.cols | <tidy-select> | everything() | Columns to transform. Accepts column names, positions, or selection helpers. |
.fns | <list> / <function> | NULL | Functions to apply. Can be a single function, named list, or unnamed list of functions. |
.names | <string> | NULL | Formula for naming output columns. Uses {col} and {fn} glue syntax. |
.predicate | <function> | — | A function that returns TRUE/FALSE for each column. |
... | <arguments> | — | Additional arguments passed to .fns. |
Examples
Basic usage with mutate
Apply mean() to all numeric columns to center them around zero.
library(dplyr)
df <- tibble(
a = c(1, 3, 5),
b = c(2, 4, 6),
name = c("Alice", "Bob", "Carol")
)
df |> mutate(across(where(is.numeric), ~ .x - mean(.x)))
#> # A tibble: 3 × 3
#> a b name
#> <dbl> <dbl> <chr>
#> 1 -2 -2 Alice
#> 2 0 0 Bob
#> 3 2 2 Carol
Applying multiple functions
Apply both mean() and sd() to numeric columns simultaneously.
df <- tibble(
x = c(1, 2, 3, 4, 5),
y = c(10, 20, 30, 40, 50),
z = c("a", "b", "c", "d", "e")
)
df |> summarise(across(where(is.numeric), list(mean = mean, sd = sd)))
#> # A tibble: 1 × 4
#> x_mean x_sd y_mean y_sd
#> <dbl> <dbl> <dbl> <dbl>
#> 1 3 1.4 30 15
Custom naming with .names
Use the .names argument to control output column names.
df <- tibble(
score1 = c(85, 92, 78),
score2 = c(90, 88, 95),
name = c("Alice", "Bob", "Carol")
)
df |> mutate(across(starts_with("score"), round, .names = "{col}_rounded"))
#> # A tibble: 3 × 5
#> score1 score2 name score1_rounded score2_rounded
#> <dbl> <dbl> <chr> <dbl> <dbl>
#> 1 85 90 Alice 85 90
#> 2 92 88 Bob 92 88
#> 3 78 95 Carol 78 95
Using where() with other verbs
where() works with any tidy-select verb like select(), mutate(), and summarise().
df <- tibble(
id = 1:3,
age = c(25, 30, 35),
salary = c(50000, 60000, 70000),
name = c("A", "B", "C")
)
# Select all numeric columns
df |> select(where(is.numeric))
#> # A tibble: 3 × 3
#> id age salary
#> <int> <dbl> <dbl>
#> 1 1 25 50000
#> 2 2 30 60000
#> 3 3 35 70000
# Summarise all numeric columns
df |> summarise(across(where(is.numeric), sum))
#> # A tibble: 1 × 3
#> id age salary
#> <int> <dbl> <dbl>
#> 1 6 90 180000
Common Patterns
Scaling all numeric columns
# Standardize (z-score) all numeric columns
df <- tibble(
a = c(1, 2, 3, 4, 5),
b = c(10, 20, 30, 40, 50)
)
df |> mutate(across(where(is.numeric), ~ (.x - mean(.x)) / sd(.x)))
Converting character columns to factors
df <- tibble(
name = c("Alice", "Bob", "Carol"),
score = c(85, 92, 78)
)
df |> mutate(across(where(is.character), factor))
Conditional transformations
Apply different functions to different column types.
df <- tibble(
a = c(1.5, 2.5, 3.5),
b = c(4.1, 5.2, 6.3),
name = c("x", "y", "z")
)
df |> mutate(
across(where(is.double), floor),
across(where(is.character), toupper)
)