R中的数据的条件求和 [英] Conditional Summing Across data.frames in R

查看:469
本文介绍了R中的数据的条件求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Excel中,我有一个工作表(状态),执行sumifs函数,从state中具有相同状态/周组合的另一个工作表(member)的值相加。



我想这样做在R中,其中state和member是data.frames。因此,对于statedata.frame中的所有行,我想将来自member



数据集

  state = data.frame(state = c('MD','MD','MD','NY','NY','NY'),week = 1:3)
member = data。 frame(memID = 1:5,state = c('MD','MD','NY','NY','MD'),
week = 1:3,
value = c (24,43,34,54,33,35,33,11,42,23,14,12,42,4,23))

期望的输出

  state = data.frame(state = c 'MD','MD','MD','NY','NY','NY'),week = 1:3,
total = c(80,90,70,96,15,76 )

谢谢!






编辑:



我最初简化了我的例子,这个sumifs也考虑到了多重不平等所以在这个例子中,使用相同的数据,如果值在20到40之间,那么我该如何做总计呢?



新的所需输出将是

  state = data.frame (state = c('MD','MD','MD','NY','NY','NY'),week = 1:3,
total = c(80,33,58, 0,0,34))
state


解决方案

尝试

  aggregate(value〜state + week,member,sum)
/ pre>

  library(data.table)#v1 .9.5+ 
setDT(member)[,list(total = sum(value)),list(state,week)]



更新



如果您需要获得总和 20/40。

  setDT(member)[,sum(value [between(value,20,40)]),list (州,周)] 


I'm working on migrating analysis I do in Excel into R as my dataset is hitting the limits of Excel.

In Excel, I have a worksheet ("state") that performs a sumifs function, summing values from another worksheet ("member) that have the same state/week combination in "state".

I would like to do this in R, where "state" and "member" are data.frames. So, for all rows in the "state" data.frame, I would like to sum all the rows from the "member" data.frame that have the same state/week combination in "state".

Dataset

state=data.frame(state=c('MD','MD','MD','NY','NY','NY'), week = 1:3) 
member=data.frame(memID = 1:5, state = c('MD','MD','NY','NY','MD'),
              week = 1:3,
              value = c(24,43,34,54,33,35,33,11,42,23,14,12,42,4,23))

Desired Output

state = data.frame(state=c('MD','MD','MD','NY','NY','NY'), week = 1:3, 
              total = c(80,90,70,96,15,76))

Thank you!


Edit:

I oversimplified my example a bit too much initially - the sumifs also take into account multiple inequalities. So in this example, with the same data, how would I do a total where I only take the sum if the value is between 20 and 40?

The new Desired Output would be

state = data.frame(state=c('MD','MD','MD','NY','NY','NY'), week = 1:3, 
              total = c(80,33,58,0,0,34)    )
              state

解决方案

Try

 aggregate(value~state+week, member, sum)

Or

 library(data.table)#v1.9.5+
 setDT(member)[, list(total=sum(value)), list(state, week)]

Update

If you need to get the sum for 'value' between 20 and 40.

setDT(member)[, sum(value[between(value,20,40)]) ,list(state, week)]

这篇关于R中的数据的条件求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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