# [R] understanding output of tapply/by cumsum

Gerrit Draisma gdraisma at xs4all.nl
Tue Dec 7 12:39:27 CET 2010

```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 * d\$a + d\$b +0.1* d\$c
Sn<-by(d\$n,list(d\$a,d\$c),cumsum)
str(Sn)
---------
List of 10
\$ : num [1:3] 11.1 23.2 36.3
\$ : num [1:3] 21.1 43.2 66.3
\$ : num [1:3] 31.1 63.2 96.3
\$ : num [1:3]  41.1  83.2 126.3
\$ : num [1:3]  51.1 103.2 156.3
\$ : num [1:3] 11.2 23.4 36.6
\$ : num [1:3] 21.2 43.4 66.6
\$ : num [1:3] 31.2 63.4 96.6
\$ : num [1:3]  41.2  83.4 126.6
\$ : num [1:3]  51.2 103.4 156.6
- attr(*, "dim")= int [1:2] 5 2
- attr(*, "dimnames")=List of 2
..\$ : chr [1:5] "1" "2" "3" "4" ...
..\$ : chr [1:2] "1" "2"
- attr(*, "call")= language by.default(data = d\$n, INDICES = list(d\$a,
d\$c), FUN = cumsum)
- attr(*, "class")= chr "by
---------
# these give (a) lists of one numerical vector(a)
Sn[5,2]
Sn[cbind(d\$a,d\$c)]
# how to access the individual cumsum values?
# and assign them to d\$Sn?
--------------

Thanks,
Gerrit.

---
Gerrit Draisma
Department of Public Health
Erasmus MC, University Medical Center Rotterdam
Room AE-235
P.O. Box 2040 3000 CA  Rotterdam The Netherlands
Phone: +31 10 7043787 Fax: +31 10 7038474
http://mgzlx4.erasmusmc.nl/pwp/?gdraisma

```