0

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
Simon
  • 991
  • 8
  • 30
  • 2
    What happens if values in `foo` within the same group are different? – Maël Aug 01 '23 at 11:36
  • Thanks for the question, it's a good one. In my use case, that'll never actually occur. So in the above example it would be a case of "pick the first value" (which in practice will always be identical.) – Simon Aug 01 '23 at 11:37
  • 1
    That said, this is the canonical way in `dplyr`: `data %>% summarise(across(starts_with("bar"), sum), across(-starts_with("bar"), first), .by = no)`. – Maël Aug 01 '23 at 11:37

0 Answers0