本文介绍了按组累计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于以下数据集:
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
..谁能建议一个更优雅的人比此)方法按组计算累积值总数的方法?
.. 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为基
这篇关于按组累计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!