Skip to content
Prev 312102 / 398502 Next

Using cumsum with 'group by' ?

On Nov 27, 2012, at 14:30 , TheRealJimShady wrote:

            
OK. ave() is originally designed to replace observations by their per-group average. It works by splitting the input vector into groups according to one or more factors, performing FUN on each group, and putting the result back in the original positions. The default for FUN is mean, but it doesn't need to be a scalar function; it also works with a FUN that returns a vector the same length as the input, such as FUN=cumsum.

The specification does not seem to allow a list() of grouping factors. It does work, but as it is unauthorised, it might not keep working.

Elaborating on the help page example:
[1] 44.55556 44.55556 44.55556 44.55556 44.55556 44.55556 44.55556 44.55556
 [9] 44.55556 24.00000 24.00000 24.00000 24.00000 24.00000 24.00000 24.00000
[17] 24.00000 24.00000 24.55556 24.55556 24.55556 24.55556 24.55556 24.55556
[25] 24.55556 24.55556 24.55556 28.22222 28.22222 28.22222 28.22222 28.22222
[33] 28.22222 28.22222 28.22222 28.22222 28.77778 28.77778 28.77778 28.77778
[41] 28.77778 28.77778 28.77778 28.77778 28.77778 18.77778 18.77778 18.77778
[49] 18.77778 18.77778 18.77778 18.77778 18.77778 18.77778
[1] 401 401 401 401 401 401 401 401 401 216 216 216 216 216 216 216 216 216 221
[20] 221 221 221 221 221 221 221 221 254 254 254 254 254 254 254 254 254 259 259
[39] 259 259 259 259 259 259 259 169 169 169 169 169 169 169 169 169
[1]  26  56 110 135 205 257 308 334 401  18  39  68  85  97 115 150 180 216  36
[20]  57  81  99 109 152 180 195 221  27  41  70  89 118 149 190 210 254  42  68
[39]  87 103 142 170 191 230 259  20  41  65  82  95 110 125 141 169
[1]  26  56 110 135 205 257 308 334 401  18  39  68  85  97 115 150 180 216  36
[20]  57  81  99 109 152 180 195 221  27  41  70  89 118 149 190 210 254  42  68
[39]  87 103 142 170 191 230 259  20  41  65  82  95 110 125 141 169