How to aggregate some columns while keeping other columns in R?

后端 未结 3 718
粉色の甜心
粉色の甜心 2020-12-11 01:13

I have a data frame like this:

     id  no  age
1    1   7   23
2    1   2   23
3    2   1   25
4    2   4   25
5    3   6   23
6    3   1   23
3条回答
  •  攒了一身酷
    2020-12-11 01:59

    Alternatively, you could use ddply from plyr package:

    require(plyr)
    ddply(df,.(id,age),summarise,no = sum(no))
    

    In this particular example the results are identical. However, this is not always the case, the difference between the both functions is outlined here. Both functions have their uses and are worth exploring, which is why I felt this alternative should be mentioned.

提交回复
热议问题