across() makes it easy to apply the same transformation to multiple
columns, allowing you to use select() semantics inside in summarise() and
mutate(). across() supersedes the family of "scoped variants" like
summarise_at(), summarise_if(), and summarise_all(). See
vignette("colwise") for more details.
c_across() is designed to work with rowwise() to make it easy to
perform row-wise aggregations. It has two differences from c():
It uses tidy select semantics so you can easily select multiple variables.
See vignette("rowwise") for more details.
It uses vctrs::vec_c() in order to give safer outputs.
across(.cols = everything(), .fns = NULL, ..., .names = NULL) c_across(cols = everything())
| .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 | < |
A tibble with one column for each column in .cols and each function in .fns.
# across() ----------------------------------------------------------------- iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), mean))#>#> # A tibble: 3 x 3 #> Species Sepal.Length Sepal.Width #> <fct> <dbl> <dbl> #> 1 setosa 5.01 3.43 #> 2 versicolor 5.94 2.77 #> 3 virginica 6.59 2.97#> # A tibble: 150 x 5 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <chr> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> 7 4.6 3.4 1.4 0.3 setosa #> 8 5 3.4 1.5 0.2 setosa #> 9 4.4 2.9 1.4 0.2 setosa #> 10 4.9 3.1 1.5 0.1 setosa #> # … with 140 more rows# A purrr-style formula iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), ~mean(.x, na.rm = TRUE)))#>#> # A tibble: 3 x 3 #> Species Sepal.Length Sepal.Width #> <fct> <dbl> <dbl> #> 1 setosa 5.01 3.43 #> 2 versicolor 5.94 2.77 #> 3 virginica 6.59 2.97# A named list of functions iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd)))#>#> # A tibble: 3 x 5 #> Species Sepal.Length_mean Sepal.Length_sd Sepal.Width_mean Sepal.Width_sd #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.01 0.352 3.43 0.379 #> 2 versicolor 5.94 0.516 2.77 0.314 #> 3 virginica 6.59 0.636 2.97 0.322# Use the .names argument to control the output names iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), mean, .names = "mean_{.col}"))#>#> # A tibble: 3 x 3 #> Species mean_Sepal.Length mean_Sepal.Width #> <fct> <dbl> <dbl> #> 1 setosa 5.01 3.43 #> 2 versicolor 5.94 2.77 #> 3 virginica 6.59 2.97iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd), .names = "{.col}.{.fn}"))#>#> # A tibble: 3 x 5 #> Species Sepal.Length.mean Sepal.Length.sd Sepal.Width.mean Sepal.Width.sd #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.01 0.352 3.43 0.379 #> 2 versicolor 5.94 0.516 2.77 0.314 #> 3 virginica 6.59 0.636 2.97 0.322iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), list(mean, sd), .names = "{.col}.fn{.fn}"))#>#> # A tibble: 3 x 5 #> Species Sepal.Length.fn1 Sepal.Length.fn2 Sepal.Width.fn1 Sepal.Width.fn2 #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.01 0.352 3.43 0.379 #> 2 versicolor 5.94 0.516 2.77 0.314 #> 3 virginica 6.59 0.636 2.97 0.322# c_across() --------------------------------------------------------------- df <- tibble(id = 1:4, w = runif(4), x = runif(4), y = runif(4), z = runif(4)) df %>% rowwise() %>% mutate( sum = sum(c_across(w:z)), sd = sd(c_across(w:z)) )#> # A tibble: 4 x 7 #> # Rowwise: #> id w x y z sum sd #> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 1 0.0808 0.00740 0.733 0.0342 0.855 0.347 #> 2 2 0.834 0.466 0.773 0.320 2.39 0.245 #> 3 3 0.601 0.498 0.875 0.402 2.38 0.204 #> 4 4 0.157 0.290 0.175 0.196 0.818 0.0590