Search code examples
rloopscumsum

Cumulative sum based on factor on R


I have the following dataset, and I need to acumulate the value and sum, if the factor is 0, and then put the cummulated sum when I found the factor != 0.

I've tried the loop bellow, but it didn't worked at all.

for(i in dataset$Variable.1) {

  ifelse(dataset$Factor == 0, 
     dataset$teste <- dataset$Variable.1 + i, 
     dataset$teste <- dataset$Variable.1) 
  i<- dataset$Variable.1
  print(i)
}

Any ideas?

Bellow an example of the dataset. I wish to get the "Result" Column.

On the real one, I also have a negative factor (-1).

     Date       Factor  Variable.1  Result
1  03/02/2018      0       0.75   0.75
2  04/02/2018      0       0.75   1.50
3  05/02/2018      1       0.96   2.46
4  06/02/2018      1       0.76   0.76
5  07/02/2018      0       1.35   1.35
6  08/02/2018      1       0.70   2.05
7  09/02/2018      1       2.02   2.02
8  10/02/2018      0       0.00   0.00
9  11/02/2018      0       0.00   0.00
10 12/02/2018      0       0.20   0.20
11 13/02/2018      0       0.13   0.33
12 14/02/2018      0       1.64   1.97
13 15/02/2018      0       0.03   2.00
14 16/02/2018      1       0.51   2.51
15 17/02/2018      1       0.00   0.00
16 18/02/2018      0       0.00   0.00
17 19/02/2018      0       0.83   0.83
18 20/02/2018      1       0.42   1.25
19 21/02/2018      1       0.17   0.17
20 22/02/2018      1       0.97   0.97
21 23/02/2018      0       0.92   0.92
22 24/02/2018      0       0.00   0.92
23 25/02/2018      0       0.00   0.92
24 26/02/2018      1       0.19   1.11
25 27/02/2018      1       0.87   0.87
26 28/02/2018      1       0.85   0.85
27 01/03/2018      1       1.95   1.95
28 02/03/2018      1       0.54   0.54
29 03/03/2018      1       0.00   0.00
30 04/03/2018      0       0.00   0.00
31 05/03/2018      0       1.17   1.17
32 06/03/2018      1       0.25   1.42
33 07/03/2018      1       1.45   1.45    

Thanks In advance.


Solution

  • If you want to stick with the for-loop, you can try this code :

    DF$Result <- NA
    prev <- 0
    for(i in seq_len(nrow(DF))){
      DF$Result[i] <- DF$Variable.1[i] + prev
      if(DF$Factor[i] == 1)
        prev <- 0
      else
        prev <- DF$Result[i]
    }