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

From: Marc Schwartz <marc_schwartz_at_me.com>
Date: Fri, 22 Apr 2011 13:44:42 -0500

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

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. Received on Fri 22 Apr 2011 - 18:46:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 22 Apr 2011 - 18:50:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.