2

I am hoping to consolidate data in several columns to group by unique value in another column.

group_id food_1 food_2 food_3
1 1 0 0
1 0 2 0
1 0 0 6
2 2 0 0
2 0 1 0
2 0 0 5

I would like it be consolidated so it is one row for each group_id.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81

1 Answers1

3

In base R we can use aggregate: With aggregate we can apply the sum function to each column groupwise:

aggregate(. ~ group_id, df, sum)

the dplyr equivalent would be:

library(dplyr) #>= 1.1.0
df %>% 
  summarise(across(starts_with("food"), ~sum(.)), .by = group_id)
  group_id food_1 food_2 food_3
1        1      1      2      6
2        2      2      1      5
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    Great answer (+1)! Just a bit of code golf, but could shorten the `dplyr` approach to `summarise(across(starts_with("food"), sum), .by = group_id)` - no need to call the formula annotation here (unless there is a preferred reason I dont know about, highly likely) – jpsmith Jul 25 '23 at 20:40
  • 1
    Thanks. you are right, here we do not need anonymous function because we do not use any arguments inside. Great tip! – TarJae Jul 25 '23 at 20:49