I have input data (df) for making 2*2 contingency table for each row.
df <- data.frame(as = c("A", "B", "C", "D"), sum_m = c(47, 8, 93, 73),
length_m = c(150, 150, 150, 150), sum_w = c(66, 183, 44, 113), length_w = c(199, 199, 199, 199),
pooled_p = c(0.32378223495702, 0.547277936962751, 0.392550143266476, 0.532951289398281),
test1 = c(TRUE, TRUE, TRUE, TRUE), test2 = c(TRUE, TRUE, TRUE, TRUE), test3 = c(TRUE, TRUE, TRUE, TRUE),
test4 = c(TRUE, TRUE, TRUE, TRUE), final_test = c(TRUE, TRUE, TRUE, TRUE))
I wrote a small script (given below) for calculating p value for a single row:
# Chi-square or Fisher's exact test
x <- c(sum_m, sum_w)
n <- c(length_m, length_w)
mash <- rbind(c(sum_m, length_m - sum_m),
c(sum_w, length_w - sum_w))
if(final_test == TRUE){
## With Yate's continuity correction
prop.test(x,n)
#Exactly the same as:
chisq.test(mash)
}else{
# Fisher's exact test
fisher.test(mash)
}
hopefully, this makes sense to you.
Suggestions on how to apply this to a large number of rows would be greatly appreciated! If possible then please paste the p-value at the last column.
Thanks in advance :X)