0

I have a dataframe as follows:

ID Unique_Val Group_val
1   4          5
1   6          5
2   8          3
2   4          3

Note that the data is in a long format where the group_val is duplicated for each row with the same ID. I'd like to get output as follows:

ID Unique_Val Group_val
1  10         5
2  12         3

Where we sum over the Unique_val for each ID, but preserve the Group_val. I know I could do some nasty looping, but I imagine there is an ellegent dpylr solution I can't figure out. Seems like summarize + group_by, but can't figure it out.

Tanner Phillips
  • 392
  • 1
  • 11

2 Answers2

2
data %>% group_by(ID, Group_val) %>% summarise(Unique_Val = sum(Unique_Val))

You just need to group by your two variables.

Wawv
  • 371
  • 2
  • 6
0

Using aggregate from base R

aggregate(Unique_Val ~ ID + Group_val, data, FUN = sum)
akrun
  • 874,273
  • 37
  • 540
  • 662