从结尾到开头计算cumsum

时间:2018-05-18 14:20:30

标签: r reverse cumsum

我正在尝试计算从每一组的最后一行到第一行的cumsum

示例数据:

t1 <- data.frame(var = "a", val = c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,0))
t2 <- data.frame(var = "b", val = c(0,0,0,0,1,0,0,1,0,0,0,0,0,0,0))
ts <- rbind(t1, t2)

所需格式(按var分组):

ts <- data.frame(var = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                           "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"), 
                 val = c(2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,2,2,2,2,2,1,1,1,0,0,0,0,0,0,0))

2 个答案:

答案 0 :(得分:9)

将我的评论推荐给答案;使用:

ts$val2 <- ave(ts$val, ts$var, FUN = function(x) rev(cumsum(rev(x))))

给出:

> ts
   var val val2
1    a   0    2
2    a   0    2
3    a   0    2
4    a   0    2
5    a   1    2
6    a   0    1
7    a   0    1
8    a   0    1
9    a   0    1
10   a   1    1
11   a   0    0
12   a   0    0
13   a   0    0
14   a   0    0
15   a   0    0
16   b   0    2
17   b   0    2
18   b   0    2
19   b   0    2
20   b   1    2
21   b   0    1
22   b   0    1
23   b   1    1
24   b   0    0
25   b   0    0
26   b   0    0
27   b   0    0
28   b   0    0
29   b   0    0
30   b   0    0

dplyrdata.table

library(dplyr)
ts %>% 
  group_by(var) %>%
  mutate(val2 = rev(cumsum(rev(val))))

library(data.table)
setDT(ts)[, val2 := rev(cumsum(rev(val))), by = var]

答案 1 :(得分:1)

不显式反转向量的选项:

ave(ts$val, ts$var, FUN = function(x) Reduce(sum, x, right = TRUE, accumulate = TRUE))

 [1] 2 2 2 2 2 1 1 1 1 1 0 0 0 0 0 2 2 2 2 2 1 1 1 0 0 0 0 0 0 0

或与dplyr相同的方法:

ts %>%
 group_by(var) %>%
 mutate(val = Reduce(sum, val, right = TRUE, accumulate = TRUE))