Search code examples
rdataframegroup-byaggregatesummarize

Which is the simplest way to aggregate rows (sum) by columns values the following type of data frame on R?


index   type.x  type.y   col3   col4
1        a        m      20      25
2        b        m      30      28
3        a        m      15      555
3        a        n      20      555
4        a        m      666     10
4        b        m      666     20

I have tried aggregate keeping the index and group_by without success when I try to get this shape:

index   col3   col4
1        20      25
2        30      28
3        35      555
4        666     30

Solution

  • If you are using base R, the following code may help

    r <- aggregate(df[4:5],by = df[1],function(v) sum(unique(v)))
    

    which gives

    > r
      index col3 col4
    1     1   20   25
    2     2   30   28
    3     3   35  555
    4     4  666   30