我想通过Index对这个数据集的行进行分组,然后通过公共索引对行进行求和 [英] I would like to group the rows of this dataset by Index and then sum the rows by common index

查看:151
本文介绍了我想通过Index对这个数据集的行进行分组,然后通过公共索引对行进行求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过MemberID对这些数据集的行进行分组。

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

我想要的数据框架:

  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

然后我想总结行通过公共ID
导致以下数据框架

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.

推荐答案

假设你的数据是在一个名为 dat

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

这篇关于我想通过Index对这个数据集的行进行分组,然后通过公共索引对行进行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆