[R] Create 2x2 table from summary data and run chi square test.

Marc Schwartz marc_schwartz at me.com
Fri Apr 22 20:44:42 CEST 2011


On Apr 22, 2011, at 1:37 PM, John Sorkin wrote:

> 
> R 2.12
> windows 7
> 
> I am summary data that I would like to make into a 2x2 table representing counts positive vs. negative counts:
> 28/289  20/276
> 
> My table should look something like the following:
> 
>          group1 group2
> Positive    28     20
> Negative   289     276
> 
> How can a (1) create the 2x2 table
>          (2) run a chi square test on the table?
> 
> I have tried the following code, but I don't know if it is correct, and it does not give me an explicit 2x2 table:
> 
>> xx <- c(28,20)
>> pp <- c(317/(317+296), 296/(317+296))
>> chisq.test(xx,p=pp)
> 
>        Chi-squared test for given probabilities
> 
> data:  xx 
> X-squared = 0.8425, df = 1, p-value = 0.3587
> 


Hi John,

This is just a matrix. Thus, if you have the counts:

x <- matrix(c(28, 20, 289, 276), byrow = TRUE, 2, 2)

> x
     [,1] [,2]
[1,]   28   20
[2,]  289  276

> chisq.test(x)

	Pearson's Chi-squared test with Yates' continuity correction

data:  x 
X-squared = 0.6491, df = 1, p-value = 0.4204


or as some might prefer:

# No Yates correction, which is conservative
> chisq.test(x, correct = FALSE)

	Pearson's Chi-squared test

data:  x 
X-squared = 0.9141, df = 1, p-value = 0.339



HTH,

Marc Schwartz



More information about the R-help mailing list