简体   繁体   中英

How to calculate cumulative sum for each group in time?

For each unique ID and rep, I want to calculate the cumulative number of babies at each age?

For instance, A1, the cumulative sum should look like 1,3,6

I tried the folowing method

id <- c("A","A","A","A","A","A","B","B","B","B","B","B","B","B","B")
rep <- c(1,1,1,2,2,2,1,1,1,1,2,2,2,2,2)
age <- c(0,1,2,0,1,2,0,1,2,3,0,1,2,3,4)
babies <- c(1,2,3,0,1,3,0,1,5,1,0,0,12,1,1)

df <- data.frame(id,rep,age,babies)

df$csum <- ave(df$babies, c(df$id,df$age, df$age), FUN=cumsum)

The result is cumulative sum is calculated over ID alone but not replicate or age. Any suggestions?

How about this:

library(dplyr)
  id <- c("A","A","A","A","A","A","B","B","B","B","B","B","B","B","B")
  rep <- c(1,1,1,2,2,2,1,1,1,1,2,2,2,2,2)
  age <- c(0,1,2,0,1,2,0,1,2,3,0,1,2,3,4)
  babies <- c(1,2,3,0,1,3,0,1,5,1,0,0,12,1,1)
  
  df <- data.frame(id,rep,age,babies)
  df %>% 
    group_by(id, rep) %>% 
    arrange(age, .by_group = TRUE) %>% 
    mutate(csum = cumsum(babies))
#> # A tibble: 15 × 5
#> # Groups:   id, rep [4]
#>    id      rep   age babies  csum
#>    <chr> <dbl> <dbl>  <dbl> <dbl>
#>  1 A         1     0      1     1
#>  2 A         1     1      2     3
#>  3 A         1     2      3     6
#>  4 A         2     0      0     0
#>  5 A         2     1      1     1
#>  6 A         2     2      3     4
#>  7 B         1     0      0     0
#>  8 B         1     1      1     1
#>  9 B         1     2      5     6
#> 10 B         1     3      1     7
#> 11 B         2     0      0     0
#> 12 B         2     1      0     0
#> 13 B         2     2     12    12
#> 14 B         2     3      1    13
#> 15 B         2     4      1    14

Created on 2022-12-08 by the reprex package (v2.0.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM