[R] 3-way contingency table

From: Mathias Walter <mathias.walter_at_googlemail.com>
Date: Fri, 29 Apr 2011 12:47:31 +0200


Hi,

I have large data frame with many columns. A short example is given below:

> dataH

    host ms01 ms31 ms33 ms34
1 cattle 4 20 9 6
2 sheep 4 3 4 5
3 cattle 4 3 4 5
4 cattle 4 3 4 5
5 sheep 4 3 5 5
6 goat 4 3 4 5
7 sheep 4 3 5 5
8 goat 4 3 4 5
9 goat 4 3 4 5
10 cattle 4 3 4 5

Now I want to determine the the frequencies of every unique value in every column depending on the host column.

It is quite easy to determine the frequencies in total with the following command:

> dataH2 <- dataH[,c(2,3,4,5)]
> table(as.matrix(dataH2), colnames(dataH2)[col(dataH2)], useNA="ifany")

    ms01 ms31 ms33 ms34
 3 0 9 0 0
 4 10 0 7 0

 5     0    0    2    9
 6     0    0    0    1
 9     0    0    1    0

 20 0 1 0 0

But I cannot manage to get it dependent on the host.

I tried

> xtabs(cbind(ms01, ms31, ms33, ms34) ~ ., dataH)

and many other ways but I'm not stressful.

I can get it for each column individually with

> with(dataH, table(host, ms33))

       ms33
host     4 5 9

 cattle 3 0 1
 deer 0 0 0
 goat 3 0 0
 human 0 0 0
 sheep 1 2 0
 tick 0 0 0

But I do not want to repeat the command for every column. I need a single table which can be plotted as a balloon plot, for instance.

Does anybody knows how to achieve this?

--
Kind regards,
Mathias

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 29 Apr 2011 - 13:34:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Apr 2011 - 14:20:36 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive