如何对r中不同数据帧的值求和?

时间:2014-03-04 11:51:40

标签: r dataframe sum

您好我想按不同级别(列sum)对值(列users)求和,并在不同的数据帧中报告。有时,两个数据框中都不会报告用户 我的第一个数据框是:

> head(reshape25)
         users      sum                  L1
1 00250902DC7D 30.15000 2014-01-14 06:30:00
2 00250902DC7D 29.68333 2014-01-14 10:30:00
3 00250902DC7D 26.15000 2014-01-14 18:00:00
4 00250902DC7D 28.73333 2014-01-15 02:30:00
5 00250902FB05 28.73333 2014-01-15 02:30:00
6 002509030C41 28.73333 2014-01-15 02:30:00   

> head(reshape52)
         users      sum                  L1
1 00250902FA92  0.25000 2014-01-20 01:00:00
2 00250902FB05  0.25000 2014-01-20 01:00:00
3 002509030C41  0.25000 2014-01-20 01:00:00
4 00250902FA92  0.80000 2014-01-20 07:00:00
5 00250902FB05  0.80000 2014-01-20 07:00:00
6 002509030C41 29.68333 2014-01-20 07:00:00

2 个答案:

答案 0 :(得分:0)

如果所有数据帧具有相同的结构,您可以只组合数据集然后合并组合

library(gdata)
combined_dfs <- combine(reshape25, reshape52)
ddply(combined_dfs, .(users), summarise, sum = sum(sum))

         users       sum
1 00250902DC7D 114.71666
2 00250902FB05  29.78333
3 002509030C41  58.66666
4 00250902FA92   1.05000

答案 1 :(得分:0)

tapply(rbind(df1,df2)$sum,rbind(df1,df2)$users,sum)