用条件计算累积平均值

时间:2016-03-24 15:01:52

标签: r data.table

我有像这样的data.table

a <- data.table(
    A = c(1:6),
    B = c(1,2,2,3,4,2),
    C = c("t","t","f","t","f","t")
)
   A B C
1: 1 1 t
2: 2 2 t
3: 3 2 f
4: 4 3 t
5: 5 4 f
6: 6 2 t 

在每一行,我想计算所有先前行的平均值,但条件是它将排除C == f行。 结果应该是

   A B C D   E
1: 1 1 t NA  NA
2: 2 2 t 1   1 
3: 3 2 f 1.5 1.5
4: 4 3 t 1.5 1.5
5: 5 4 f 3.5 3
6: 6 2 t 3.5 3

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你的意思是累积均值吗?然后这应该做你要求的:

a[C != "f", lapply(.SD, FUN = function(x) {cumsum(x) / seq_along(x)}), 
  .SDcols = c("A", "B")]

          A   B
1: 1.000000 1.0
2: 1.500000 1.5
3: 2.333333 2.0
4: 3.250000 2.0