Search code examples
rcumsum

Cumulative sum of dataframe up to a certain line (row)


I would like to have cumulative sum of following data:

    c1    c2    c3
    
1     3     6     3
2     4     3     2
3     6     2     5
4     1     5     4
5     0     0     0
6     0     0     0

but up to 4th line (row). For example, a following code with produce general cumulative sum of dataframe including all the rows over the columns

library(readxl)
library(xts)
library("xlsx")
library(dplyr)
library(data.table)
library(tidyverse)

D <- structure(list(c1 = c(3, 4, 6, 1, 0, 0), c2 = c(6, 3, 2, 5, 0, 
0), c3 = c(3, 2, 5, 4, 0, 0)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))
D
csD <- cumsum(D)
csD

resulting with

     c1    c2    c3
    
1     3     6     3
2     7     9     5
3    13    11    10
4    14    16    14
5    14    16    14
6    14    16    14

However, I would like to have:

     c1    c2    c3
    
1     3     6     3
2     7     9     5
3    13    11    10
4    14    16    14
5     0     0     0
6     0     0     0

Thank you in advance. Alan


Solution

  • Maybe not the most optimal way but you can define N and use apply() and rbind() like this:

    #Code
    #Define N
    N <- 4
    #Compute
    newdf <- rbind(apply(D,2,function(x) cumsum(x[1:N])),
                   D[(N+1):nrow(D),])
    

    Output:

    newdf
      c1 c2 c3
    1  3  6  3
    2  7  9  5
    3 13 11 10
    4 14 16 14
    5  0  0  0
    6  0  0  0