重置10个值后的累积总和

问题描述 投票:0回答:4

我在h中有要在bw中求和的数据,经过10个步骤后,我想重置积和。我怎么得到这个?

   h bw
1  0  0
2  0  0
3  1  1
4  0  1
5  1  2
6  0  2
7  0  2
8  1  3
9  1  4
10 1  5
11 0  0
12 0  0
13 0  0
14 0  0
15 1  1
16 1  2
17 1  3
18 0  3
19 0  3
20 1  4
21 1  1
22 0  1

此刻,我正在使用此方法:

ff = function(x)
{
  cs = cumsum(x)
  cs - cummax((x == 0) * cs)
}

但是当h为0且不超过10个值时,它将重置。

非常感谢!

r cumsum
4个回答
1
投票

您可以将矢量按每个元素的位置除以10为模,以单线方式进行。

as.numeric(unlist(sapply(split(df$bw, (seq_along(df$bw)-1) %/% 10), cumsum)))
# [1]  0  0  1  2  4  6  8 11 15 20  0  0  0  0  1  3  6  9 12 16  1  2

0
投票
df <- structure(list(h = c(0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 
0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L), bw = c(0L, 0L, 
1L, 1L, 2L, 2L, 2L, 3L, 4L, 5L, 0L, 0L, 0L, 0L, 1L, 2L, 3L, 3L, 
3L, 4L, 1L, 1L), cs = c(0L, 0L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 5L, 
0L, 0L, 0L, 0L, 1L, 2L, 3L, 3L, 3L, 4L, 1L, 1L)), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22"), class = "data.frame")

cumsum10 <- function(x){
  idx <- seq(x)%%10
  starts <- c(1, which(idx == 0)+1)
  stops <- c(which(idx == 0), length(x))

  res <- vector("list", length(starts))
  for(i in seq(res)){
    res[[i]] <- cumsum(x[starts[i]:stops[i]])
  }
  res <- do.call("c", res)
  return(res)
}


df$cs <- cumsum10(df$bw)
df


#    h bw cs
# 1  0  0  0
# 2  0  0  0
# 3  1  1  1
# 4  0  1  2
# 5  1  2  4
# 6  0  2  6
# 7  0  2  8
# 8  1  3 11
# 9  1  4 15
# 10 1  5 20
# 11 0  0  0
# 12 0  0  0
# 13 0  0  0
# 14 0  0  0
# 15 1  1  1
# 16 1  2  3
# 17 1  3  6
# 18 0  3  9
# 19 0  3 12
# 20 1  4 16
# 21 1  1  1
# 22 0  1  2

0
投票

使用dplyr的另一个选项

library(tidyverse)
df %>% 
  mutate(id = (row_number()-1)%/%10) %>% 
  group_by(id) %>% 
  mutate(cs = cumsum(bw)) %>% 
  select(-id)

0
投票

使用ave,你可以做

bw <- ave(h, rep(1:ceiling(length(h)/10), each=10)[seq(h)], FUN=cumsum)
bw
# [1] 0 0 1 1 2 2 2 3 4 5 0 0 0 0 1 2 3 3 3 4 1 1
© www.soinside.com 2019 - 2024. All rights reserved.