[R] Setting a contingency table

David Winsemius dwinsemius at comcast.net
Sat Jan 10 21:27:59 CET 2009


  library(gmodels)
  ?CrossTable        # by Marc Schwartz

> a<-67; b<-10; c<-79; d<-67;

 > dft<- with(as.data.frame.table(as.table(c(a=77,b=10,c=79,d=67))),  
data.frame(Var1 = rep(Var1,Freq)) )

There are probably more efficient ways to replicate entries. I am just  
drawing a blank right now.

 > dft$Rw <- with(dft, ifelse((Var1 == "a" | Var1 == "c"), 1, 2))
 > dft$Cl <- with(dft, ifelse((Var1 == "a" | Var1 == "b"), 1, 2))


 > table(dft$Rw, dft$Cl)

      1  2
   1 77 10
   2 79 67

 > CrossTable(dft$Rw,dft$Cl, prop.chisq=FALSE)


    Cell Contents
|-------------------------|
|                       N |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|


Total Observations in Table:  233


              | dft$Cl
       dft$Rw |         1 |         2 | Row Total |
-------------|-----------|-----------|-----------|
            1 |        77 |        10 |        87 |
              |     0.885 |     0.115 |     0.373 |
              |     0.494 |     0.130 |           |
              |     0.330 |     0.043 |           |
-------------|-----------|-----------|-----------|
            2 |        79 |        67 |       146 |
              |     0.541 |     0.459 |     0.627 |
              |     0.506 |     0.870 |           |
              |     0.339 |     0.288 |           |
-------------|-----------|-----------|-----------|
Column Total |       156 |        77 |       233 |
              |     0.670 |     0.330 |           |
-------------|-----------|-----------|-----------|



-- 
David Winsemius
On Jan 10, 2009, at 11:32 AM, Bhargab Chattopadhyay wrote:

> Hi,
>
> I want to set a make a contingency table which will look like this..  
> The problem is that I can't  set the table like the following.
>
>
>                      col1     col2       Total
>     row1              a         b         n10
>                      rp1       rp2        100
>                      cp1       cp2       rtp1
>   ---------------------------------------------
>     row2             c         d         n20
>                      rp3       rp4       100
>                      cp3       cp4       rtp2
>   ---------------------------------------------
>     Total            n01       n02         n
>                      ctp1      ctp2       100
>                      100       100        100
>
>
> Suppose
>
> a<-67; b<-10; c<-79; d<-67;
> n<-a+b+c+d;
>   n10<-a+b;
>   n01<-a+c;
>   n20<-n-n10;
>   n02<-n-n01;
>   rp1<-(a/n10)*100;
>   rp2<-100-rp1;
>   rp3<-(c/n20)*100;
>   rp4<-100-rp3;
>   cp1<-(a/n01)*100;
>   cp2<-100-cp1;
>   cp3<-(c/n02)*100;
>   cp4<-100-cp3;
>   rtp1<-(n10/n)*100;
>   rtp2<-100-rtp1;
>   ctp1<-(n01/n)*100;
>   ctp2<-100-ctp1;
>
> After this I can't procced in a meaningful way. Can anyone help me  
> out?
> Thanks in advance.
>
> Bhargab
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.




More information about the R-help mailing list