I am trying to get the cumulative sum of a variable (v) for groups (\"a\" and \"b\") within a dataframe. How can I get the result at the bottom -- whose rows are even number
My tool of choice for these things is the plyr package:
require(plyr)
> ddply(d,.(g),transform,cs = cumsum(v))
g v cs
1 a 1 1
2 a 1 2
3 a 1 3
4 a 2 5
5 a 2 7
6 a 2 9
7 b 4 4
8 b 4 8
9 b 4 12
10 b 8 20
11 b 8 28
12 b 8 36