Cumulative amount by group

For the following data set:

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 of calculating the total number of values โ€‹โ€‹for a 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] 

.. which is rather clumsy and gives a flat matrix, which requires dplyr::gather or reshape2::melt reformat.

Sure, R can do better than that?

+6
source share
4 answers

If you just need cumulative amounts for each group, you can do

 transform(d, new=ave(value,group,FUN=cumsum)) 

with base R.

+8
source

This should work

 library(dplyr) d %>% group_by(group) %>% arrange(date) %>% mutate(Total = cumsum(value)) 
+3
source

Since this question was tagged data.table , you're probably looking for (modification of @Franks comment).

 setDT(d)[order(date), new := cumsum(value), by = group] 

This will lead to the simultaneous permutation of the data using date (not necessary, if necessary, if not, you can get rid of order(date) ) and update the data set in place using the operator :=

+3
source

It is he?

 sp <- split(d, d$group) res <- lapply(seq_along(sp), function(i) cumsum(sp[[i]]$value)) res <- lapply(seq_along(res), function(i){ sp[[i]]$c.sum <- res[[i]] sp[[i]] }) res <- do.call(rbind, res) res 
0
source

Source: https://habr.com/ru/post/987758/


All Articles