[R] pooling categories in a table

John Fox jfox at mcmaster.ca
Mon Apr 8 22:58:16 CEST 2002


Dear Mike,

At 01:53 PM 4/8/2002 -0400, Michael Friendly wrote:
>I have a large n-way contingency table, constructed as a table
>object, and want to pool (collapse) some categories, summing the
>frequencies in all collapsed cells.  How can I do this?

Use apply; for example:

     > tab <- as.table(array(1:24, c(2,3,4)))
     > tab
     , , A

     A B C
     A 1 3 5
     B 2 4 6

     , , B

     A  B  C
     A 7  9 11
     B 8 10 12

     , , C

     A  B  C
     A 13 15 17
     B 14 16 18

     , , D

     A  B  C
     A 19 21 23
     B 20 22 24

    > apply(tab, c(2,3), sum)  # sums over first coordinate
     A  B  C  D
     A  3 15 27 39
     B  7 19 31 43
     C 11 23 35 47

I hope that this helps,
  John
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list