各组累计 [英] Cumulative total by group
本文介绍了各组累计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于以下数据集:
d = data.frame(date = as.Date(as.Date('2015-01-01'):as.Date('2015-04-10'), origin = "1970-01-01"),
group = rep(c('A','B','C','D'), 25), value = sample(1:100))
head(d)
date group value
1: 2015-01-01 A 4
2: 2015-01-02 B 32
3: 2015-01-03 C 46
4: 2015-01-04 D 40
5: 2015-01-05 A 93
6: 2015-01-06 B 10
.. 任何人都可以建议一种比此 data.table 更优雅的方法来按组计算累计值) 方法?
.. can anyone advise a more elegant way to calculate a cumulative total of values by group than this data.table) method?
library(data.table)
setDT(d)
d.cast = dcast.data.table(d, group ~ date, value.var = 'value', fun.aggregate = sum)
c.sum = d.cast[, as.list(cumsum(unlist(.SD))), by = group]
.. 这很笨重,并产生一个需要 dplyr::gather
或 reshape2::melt
重新格式化的平面矩阵.
.. which is pretty clunky and yields a flat matrix that needs dplyr::gather
or reshape2::melt
to reformat.
R 肯定能做得比这更好吗??
Surely R can do better than this??
推荐答案
如果你只想要每组的累积总和,那么你可以这样做
If you just want cumulative sums per group, then you can do
transform(d, new=ave(value,group,FUN=cumsum))
以 R 为基数.
这篇关于各组累计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文