Thanks Jim,
"Ave" does what I wanted.
It is simpler and probably more efficient
than unlisting Sn as I tried.
Still I remain puzzled with the structure
of the by() or tapply() output and how
to access the individual cumsums.
Yes the split command is useful for checking
the result.
Gerrit.
Op 1
You can also use 'split' to separate each group:
> split(d, list(d$a, d$c))
$`1.1`
a b cn cum
1 1 1 1 11.1 11.1
6 1 2 1 12.1 23.2
11 1 3 1 13.1 36.3
$`2.1`
a b cn cum
2 2 1 1 21.1 21.1
7 2 2 1 22.1 43.2
12 2 3 1 23.1 66.3
$`3.1`
a b cn cum
3 3 1 1 31.1 31.1
8 3 2 1
Maybe 'ave' is what you were looking for:
> d$cum <- ave(d$n, d$a, d$c, FUN = cumsum)
> d
a b cn cum
1 1 1 1 11.1 11.1
2 2 1 1 21.1 21.1
3 3 1 1 31.1 31.1
4 4 1 1 41.1 41.1
5 5 1 1 51.1 51.1
6 1 2 1 12.1 23.2
7 2 2 1 22.1 43.2
8 3 2 1 32.1 63.2
9 4 2 1 42.1 83.2
10 5 2 1
Dear R-users,
I have a dataset with categories and numbers.
I would like to compute and add cumulative numbers
to the dataset.
I do not understand the structure of by(...) or
tapply(...) output enough to handle it.
Here a small example
--
d<-expand.grid(a=1:5,b=1:3,c=1:2)
d$n = 10 *
4 matches
Mail list logo