c_across: Combine values from multiple columns

Description Usage Arguments See Also Examples

View source: R/across.R

Description

c_across() is designed to work with rowwise() to make it easy to perform row-wise aggregations. It has two differences from c():

Usage

1
c_across(cols = everything())

Arguments

cols

<tidy-select> Columns to transform. Because across() is used within functions like summarise() and mutate(), you can't select or compute upon grouping variables.

See Also

across() for a function that returns a tibble.

Examples

1
2
3
4
5
6
7
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))
 )

Example output

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

# A tibble: 4 x 7
# Rowwise: 
     id     w     x      y      z   sum    sd
  <int> <dbl> <dbl>  <dbl>  <dbl> <dbl> <dbl>
1     1 0.286 0.290 0.576  0.954  2.11  0.315
2     2 0.774 0.680 0.215  0.324  1.99  0.271
3     3 0.448 0.390 0.0640 0.0280 0.930 0.217
4     4 0.538 0.917 0.812  0.606  2.87  0.176

dplyr documentation built on June 19, 2021, 1:07 a.m.