R tapply Function


tapply() applies a function to each cell of a ragged array.

tapply(X, INDEX, FUN = NULL, ..., simplify = TRUE)

• X: vector
• INDEX: list of one of more factors
• FUN: the function
• simplify: if true, return an array of scalar, other wise an array of list
...

>Orange    #R built-in dataset, Growth of Orange Trees
   Tree  age circumference
1     1  118            30
2     1  484            58
3     1  664            87
4     1 1004           115
5     1 1231           120
6     1 1372           142
7     1 1582           145
8     2  118            33
9     2  484            69
10    2  664           111
11    2 1004           156
12    2 1231           172
13    2 1372           203
14    2 1582           203
15    3  118            30
16    3  484            51
17    3  664            75
18    3 1004           108
19    3 1231           115
20    3 1372           139
21    3 1582           140
22    4  118            32
23    4  484            62
24    4  664           112
25    4 1004           167
26    4 1231           179
27    4 1372           209
28    4 1582           214
29    5  118            30
30    5  484            49
31    5  664            81
32    5 1004           125
33    5 1231           142
34    5 1372           174
35    5 1582           177

Calculate the mean circumference of different Tree groups:
> tapply(Orange$circumference,Orange$Tree,mean)
        3         1         5         2         4 
 94.00000  99.57143 111.14286 135.28571 139.28571 

Return a list:
> tapply(Orange$circumference,Orange$Tree,mean,simplify=FALSE)
$`3`
[1] 94

$`1`
[1] 99.57143

$`5`
[1] 111.1429

$`2`
[1] 135.2857

$`4`
[1] 139.2857