NA满足条件时的累计和

Cumulative sum while a condition is met with NA

假设我有这两个向量:

a <- c(0,0,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3)
b <- c(NA,NA,NA,3,NA,NA,5,NA,NA,4,5,NA,2,NA,1,NA,NA,1)

我正在尝试按组计算累计总和,最终结果如下:

c(NA,NA,NA,3,NA,NA,8,NA,NA,4,9,NA,11,NA,1,NA,NA,2)

我正在尝试 do.call(rbind,by(b,a,cumsum)) 但它不起作用,returns 一个错误

Warning message:
In (function (..., deparse.level = 1)  :
  number of columns of result is not a multiple of vector length (arg 1)

有什么想法吗? 谢谢!

你可以使用 ave.

ave(b, a, FUN=\(x) {r <- cumsum(replace(x, is.na(x), 0)); replace(r, is.na(x), NA)})
# [1] NA NA NA  3 NA NA  8 NA NA  4  9 NA 11 NA  1 NA NA  2

另一种可能的解决方案,基于 dplyr 和之前创建的数据框:

library(dplyr)

df <- data.frame(a, b)

df %>% 
  group_by(a) %>% 
  mutate(c = cumsum(ifelse(!is.na(b), b, 0))) %>% 
  mutate(c = ifelse(is.na(b), NA, c)) %>% 
  pull(c)

#>  [1] NA NA NA  3 NA NA  8 NA NA  4  9 NA 11 NA  1 NA NA  2