Andrew Robinson wrote:
> 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
Hi Andrew,
How about:
ui <- data.frame(burn = c("low", "high", "low",
"low"),
age = c("young", "old", "old",
"young"),
junk = c("a", "a", "b",
"b"))
ui2 <- do.call("table", ui)
ui3 <- expand.grid(dimnames(ui2))
ui3[paste(names(ui), collapse = ".")] <-
do.call("paste", c(ui3, sep = "."))
ui3$count <- c(ui2)
I'm using R-1.9.0 on win2000.
--sundar