I was just wondering how I can go about calculating the cumulative sum with conditions on a matrix. Here's what I mean: Let's say we have a matrix with a column called ID
and a column called Value
as follows:
ID | VALUE
------------------------------
2 | 50
7 | 19
5 | 32
2 | 21
8 | 56
7 | 5
7 | 12
2 | 16
5 | 42
I wish to compute the cumulative sum on this matrix based on the ID
column. This means the cumulative sum column (or vector) would look like:
ID | CUMULATIVE SUM
----------------------------------
2 | 50
7 | 19
5 | 32
2 | 71
8 | 56
7 | 24
7 | 36
2 | 87
5 | 74
Is there a way to do this? A search for this hasn't turned up much at all (I've found stuff relevant for data frames/data tables, but I haven't found anything at all when it comes to 'conditions' with matrices), so any help would be appreciated.