[R] 3-way contingency table
Mathias Walter
mathias.walter at googlemail.com
Fri Apr 29 12:47:31 CEST 2011
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
More information about the R-help
mailing list