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.
Arguments
- cols
<
tidy-select
> Columns to transform. You can't select grouping columns because they are already automatically handled by the verb (i.e.summarise()
ormutate()
).
See also
across()
for a function that returns a tibble.
Examples
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 × 7
#> # Rowwise:
#> id w x y z sum sd
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 0.790 0.588 0.142 0.225 1.74 0.305
#> 2 2 0.892 0.514 0.781 0.207 2.39 0.305
#> 3 3 0.327 0.317 0.456 0.659 1.76 0.159
#> 4 4 0.351 0.408 0.234 0.715 1.71 0.205