user35131
user35131

Reputation: 1134

Using R, how to have the chisq.test function read the 4 column entries to get the proper statistics

I have this dataset

structure(list(`total primary - yes RS` = 138L, `total primary - no RS` = 29L, 
`total secondary- yes rs` = 6L, `total secondary- no rs` = 0L), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame"))


`total primary - yes RS`|`total primary - no RS`|`total secondary- yes rs`|`total secondary- no rs`
                   138                      29                         6                      0

I'm not asking to convert it like this but use this as a reference for alignment of the dataset

                   Col1                     Col2
 Row1 `total primary - yes RS|`total secondary- yes rs`
 Row2 `total primary - no RS`|`total secondary- no rs`

                   Col1                     Col2 
 Row1                 138                         6
 Row2                  29                         0

I tried to run this but i don't how i should order this. I got the wrong result using the dataset like this.

 chisq.test(sample[ , c("total primary - yes RS" , "total primary - no RS"  , "total secondary- yes rs" ,"total secondary- no rs")])

and the results i expect are

X-squared = 0.31654, df = 1, p-value = 0.5737
       

Upvotes: 1

Views: 32

Answers (1)

akrun
akrun

Reputation: 887501

It is easier to unlist and create a matrix of dim 2 x 2 before applying the chisq.test

chisq.test(matrix(unlist(df1), ncol  = 2))
#   Pearson's Chi-squared test with Yates' continuity correction

#data:  matrix(unlist(df1), ncol = 2)
#X-squared = 0.31654, df = 1, p-value = 0.5737

data

df1 <- structure(list(`total primary - yes RS` = 138L, `total primary - no RS` = 29L, 
    `total secondary- yes rs` = 6L, `total secondary- no rs` = 0L), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 1

Related Questions