I would like test to sum the columns for individual rows, not all the rows combined. I also tried across(starts_with(), sum)
but that seems to produce all the rows summed for each of the columns instead.
Essentially, how can I achieve testt = c(5, 10)
?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(stringr)
df = data.frame(xx_1 = c(1, 2),
xx_2 = c(1, 2),
xx_3 = c(1, 2),
xx_4 = c(1, 2),
xx_5 = c(1, 2),
yy_1 = c(3, 4))
df |> mutate(testt = sum(across(starts_with('xx_'))))
#> xx_1 xx_2 xx_3 xx_4 xx_5 yy_1 testt
#> 1 1 1 1 1 1 3 15
#> 2 2 2 2 2 2 4 15
Created on 2021-12-03 by the reprex package (v2.0.1)