[R] pooling categories in a table

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Apr 9 00:49:19 CEST 2002


John Fox <jfox at mcmaster.ca> writes:

> 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)))
..
>     > 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,

margin.table does this too. However, I'm afraid this wasn't the
question. I suspect it was to reduce, say, the 3rd classifier from
four levels to two. 

This is more tricky, but at least with xtabs objects you can do things
like this:

 data(UCBAdmissions)
 DF <- as.data.frame(UCBAdmissions)
 DF$Dept <- factor(c(1,2,2,3,4,5)[DF$Dept],
     labels=c("A","B+C","D","E","F"))
 xtabs(Freq~.,DF)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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