Suppose you have a tibble()
which looks something like:
data<-tibble(no = c(1,2,3,3), foo = c(4, 5, 6, 6), bar_1 = c(5, 6, 7, 3), bar_2 = c(4, 3, 6, 8))
# A tibble: 4 × 4
no foo bar_1 bar_2
<dbl> <dbl> <dbl> <dbl>
1 1 4 5 4
2 2 5 6 3
3 3 6 7 6
4 3 6 3 8
How can you take the sum for each group in no
for any column that begins with bar
? Columns which aren't prepended with bar
should be left as-is.
Expected output:
# A tibble: 3 × 4
no foo bar_1 bar_2
<dbl> <dbl> <dbl> <dbl>
1 1 4 5 4
2 2 5 6 3
3 3 6 10 14