[R] Aggregate drops empty subsets

Andrew Robinson andrewr at uidaho.edu
Wed Apr 14 16:29:29 CEST 2004

Greetings, R community.

I am trying to create a multi-dimensional contingency table suitable
for analysis by glm() using the poisson family.  I have three factors,
each with four levels, with some observed zeros.  I'm trying to use
aggregate to construct my contingency table, but it drops empty
subsets, so the zeros get lost.  I also tried tapply() but it doesn't
carry over the main effects, just the interactions.  I also tried
constructing a new factor from the interactions and merging it with
the contingency table but then I lost the main effects.  

Very small example: from the following dataframe

burn  age

low   young
high  old
low   old
low   young

I would want to distill

burn  age    burn.age     count
low   young  low.young    2
high  young  high.young   0
low   old    low.old      1
high  old    high.old     1

with a solution scaleable to many dimensions.

Is there any easy way to get around this problem?

Thanks for any suggestions,

Andrew Robinson                      Ph: 208 885 7115
Department of Forest Resources       Fa: 208 885 6226
University of Idaho                  E : andrewr at uidaho.edu
PO Box 441133                        W : http://www.uidaho.edu/~andrewr
Moscow ID 83843                      Or: http://www.biometrics.uidaho.edu
No statement above necessarily represents my employer's opinion.

More information about the R-help mailing list