Summarize contingency table in R I know table can count contingency table, what if I want to summarize all elements that fall in a cell of the table using some type of function, such as mean?
For example
facA = rep(1:3, 4)

facB = rep(c(rep(1, 3), rep(2, 3)), 2)

value = rnorm(12)

How do I summarize the mean of 'value's corresponding to facA:facB and make it as a table?
 A: Like this:
> facA = rep(1:3, 4)
> facB = rep(c(rep(1, 3), rep(2, 3)), 2)
> value = rnorm(12)
> aggregate(value~facA+facB, FUN=mean)
  facA facB       value
1    1    1 -1.24201923
2    2    1  0.32787424
3    3    1 -0.57436081
4    1    2  0.01640463
5    2    2 -0.91542237
6    3    2  0.27423418
> ag <- aggregate(value~facA+facB, FUN=mean)
> xtabs(value~., data=ag)
    facB
facA           1           2
   1 -1.24201923  0.01640463
   2  0.32787424 -0.91542237
   3 -0.57436081  0.27423418

A: The results are not exactly presented in a table but an easy way to do it is to use the ddply function in the plyr package.
df <- data.frame(facA,facB,value)
ddply(df, facA ~ facB, mean)

Results will look like
   facA facB value
1    1    1  0.8790879
2    1    2  0.6707739
3    2    1 -0.6817800
4    2    2 -0.6502067
5    3    1  1.5049416
6    3    2 -0.5960527

A: As the question asker noted... another approach to the answer is to use tapply.
facA = rep(1:3, 4)
facB = rep(c(rep(1, 3), rep(2, 3)), 2)
value = rnorm(12)
df <- data.frame(facA=facA,facB=facB,value=value)
with(df,tapply(value,list(facA,facB),mean))

