Sergey Bombin Sergey Bombin - 9 months ago 60
R Question

How to loop chi sq test in R

I have the table with alternative and reference allele counts. How could I loop chi sq test in R to run it for each row? I attached the picture of my table. I need to perform chi sq test with altCount and refCount columns.

altCount refCount
8 6
3 7
4 9

I need the p-value for each row. I am very new to R. I figured out how to perform chi sq test on individual rows but since I have several thousands rows I need to make a loop to run it all at once.
I did:

bcz <- read.delim("C:/cygwin64/home/sbomb/tables/bc_z_alleles.csv")
a = bcz[1,6]
b = bcz[1,7]
c = c(a,b)
d = c(0.5,0.5)
x = c,
p = d,

but I do not know how to loop it for whole table. Could you please explain me how to do it with all details?

enter image description here


The easiest loop for a beginner still is the for loop:

d <- data.frame(a = c(8,3,4), b = c(6,7,9))
for(row in 1:nrow(d)){

The same can be done with

apply(d, 1, chisq.test)

The latter is shorter and gives you a list as your result, which is probalby better for further evaluation.