I would like to group the rows of this dataset by Index and then sum the rows by common index

故事扮演 提交于 2019-12-23 03:12:52

问题


I would like to group the rows of this dataset by MemberID.

This is a snipet of my dataset "Claims":

  MemberID    SopLos    DIH
1     54         0       1
2     55         1       2
3     56         2       3  
4     67         0       5
5     55         1       1
6     54         0       1
7     55         1       2
8     56         2       3  
9     67         0       5
10    55         1       1

My desired data frame:

  MemberID    SopLos    DIH
1     54         0       1
2     54         0       1
3     55         1       1
4     55         1       2
5     55         1       1
6     55         1       2
7     56         2       3  
8     56         2       3  
9     67         0       5
10    67         0       5

Then I would like to sum the rows by common ID's resulting in the following data frame

 MemberID    SopLos    DIH
1     54         0       2
2     55         4       6  
3     56         4       6  
4     67         0       10

If you could give me any help I would greatly appreciate it.


回答1:


Assuming your data is in a dataframe called dat:

require(plyr)
ddply(dat,.(MemberID),summarise,SopLos = sum(SopLos),DIH = sum(DIH))
  MemberID SopLos DIH
1       54      0   2
2       55      4   6
3       56      4   6
4       67      0  10



回答2:


Since I only have base functions available at the moment, here's another solution. Assuming your data is in a dataframe called df:

aggregate(df[c("SopLos","DIH")],list(MemberId = df$MemberID),sum)

  MemberId SopLos DIH
1       54      0   2
2       55      4   6
3       56      4   6
4       67      0  10

If you have a lot of variables to sum or a dataset that changes frequently and don't you wish to type out all the names, you could also try:

aggregate(df[-(grep("^MemberID$",names(df)))],list(MemberId = df$MemberID),sum)


来源:https://stackoverflow.com/questions/9593056/i-would-like-to-group-the-rows-of-this-dataset-by-index-and-then-sum-the-rows-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!