Adding on to what's already being said or commented on this post: Cumulative sum until maximum reached, then repeat from zero in the next row
I've a similar dataframe which has about 50k+ observations. This dataframe was being read from a csv file and is an outcome of several operations already performed on it. Pasting a sample here:
Home Date Time Appliance Run value
679 2 1/21/2017 1:30:00 0 1 0
680 2 1/21/2017 1:45:00 0 1 0
681 2 1/21/2017 2:00:00 0 1 0
682 2 1/21/2017 2:15:00 0 1 0
683 2 1/21/2017 2:30:00 804 0 1
684 2 1/21/2017 2:45:00 556 0 804
685 2 1/21/2017 3:00:00 844 0 1360
686 2 1/21/2017 3:15:00 396 0 2204
687 2 1/21/2017 3:30:00 392 0 2600
688 2 1/21/2017 3:45:00 1220 0 2992
689 2 1/21/2017 4:00:00 0 1 0
690 2 1/21/2017 4:15:00 0 1 0
691 2 1/21/2017 4:30:00 0 1 0
692 2 1/21/2017 4:45:00 0 1 0
783 2 1/22/2017 3:30:00 0 1 0
784 2 1/22/2017 3:45:00 244 0 4212
785 2 1/22/2017 4:00:00 1068 0 4456
786 2 1/22/2017 4:15:00 44 0 5524
787 2 1/22/2017 4:30:00 1240 0 5568
788 2 1/22/2017 4:45:00 40 0 6808
789 2 1/22/2017 5:00:00 1608 0 6848
790 2 1/22/2017 5:15:00 0 1 0
791 2 1/22/2017 5:30:00 0 1 0
The code I used, given as one of the answers, df = transform(df, value = ave(Appliance, Run, FUN = function(x)c(1, head(cumsum(x),-1))))
.
However, as you can see in the output, the sums won't restart at the next occurrence of 0 plus the last sum for first group (683-688 index) is carry forwarded to 784 (index number). Please help me in restarting the sums at next occurrence of 0.
Expected output:
Home Date Time Appliance Run value
679 2 1/21/2017 1:30:00 0 1 0
680 2 1/21/2017 1:45:00 0 1 0
681 2 1/21/2017 2:00:00 0 1 0
682 2 1/21/2017 2:15:00 0 1 0
683 2 1/21/2017 2:30:00 804 0 804
684 2 1/21/2017 2:45:00 556 0 1360
685 2 1/21/2017 3:00:00 844 0 2204
686 2 1/21/2017 3:15:00 396 0 2600
687 2 1/21/2017 3:30:00 392 0 2992
688 2 1/21/2017 3:45:00 1220 0 4212
689 2 1/21/2017 4:00:00 0 1 0
690 2 1/21/2017 4:15:00 0 1 0
691 2 1/21/2017 4:30:00 0 1 0
692 2 1/21/2017 4:45:00 0 1 0
783 2 1/22/2017 3:30:00 0 1 0
784 2 1/22/2017 3:45:00 244 0 244
785 2 1/22/2017 4:00:00 1068 0 1312
786 2 1/22/2017 4:15:00 44 0 1356
787 2 1/22/2017 4:30:00 1240 0 2596
788 2 1/22/2017 4:45:00 40 0 2636
789 2 1/22/2017 5:00:00 1608 0 4244
790 2 1/22/2017 5:15:00 0 1 0
791 2 1/22/2017 5:30:00 0 1 0
P.S: I've already tried this too: Sum until a given value is reached