Skip to content

Setting a contingency table

2 messages · Bhargab Chattopadhyay, David Winsemius

#
library(gmodels)
  ?CrossTable        # by Marc Schwartz
> 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 |           |
-------------|-----------|-----------|-----------|