R根据数据帧中的1列求和值

时间:2016-12-12 11:16:40

标签: r

我有一个包含这些变量的数据框:

     Datum value  morph sex product
2016-04-05     5 Winter   M     ACV
2016-04-05     1 Summer   M     ACV
2016-04-05    18 Winter   F     ACV
2016-04-05     3 Summer   F     ACV
2016-04-05     0 Winter   M     FRA
2016-04-05     0 Summer   M     FRA
2016-04-05     0 Winter   F     FRA
2016-04-05     0 Summer   F     FRA
2016-04-08     0 Winter   M     ACV
2016-04-08     0 Summer   M     ACV

我想总结F和M的值,但保持变形和产品的单独值。现在我每个日期有8个值,我应该去4。 我该怎么做?

我读过关于tapply,ddplyr和aggregate的内容,但我要么不理解它们,要么它们不能解决我的问题......

总结我知道如何做的事情,但不能保留其他列。

1 个答案:

答案 0 :(得分:1)

这只是对一列或一组列进行分组和求和的示例

with ddply

> ddply(df, c("morph", "sex", "product"), summarise, sum_values = sum(value))
   morph sex product ..1
1 Summer   F     ACV   3
2 Summer   F     FRA   0
3 Summer   M     ACV   1
4 Summer   M     FRA   0
5 Winter   F     ACV  18
6 Winter   F     FRA   0
7 Winter   M     ACV   5
8 Winter   M     FRA   0

with dplyr()

library(dplyr)
> df %>% group_by(morph, sex, product) %>% summarise("sum_values"  = sum(value))
   morph   sex product sum_values
   (chr) (chr)   (chr)      (int)
1 Summer     F     ACV          3
2 Summer     F     FRA          0
3 Summer     M     ACV          1
4 Summer     M     FRA          0
5 Winter     F     ACV         18
6 Winter     F     FRA          0
7 Winter     M     ACV          5
8 Winter     M     FRA          0

with data.table

df[ , .(sum_values = sum(value)),by = c("morph", "sex", "product")]
    morph sex product sum_values
1: Winter   M     ACV          5
2: Summer   M     ACV          1
3: Winter   F     ACV         18
4: Summer   F     ACV          3
5: Winter   M     FRA          0
6: Summer   M     FRA          0
7: Winter   F     FRA          0
8: Summer   F     FRA          0