如何在R中循环卡方检验

How to loop chi sq test in R

我有 table 备选和参考等位基因计数。我如何将 R 中的 chi sq test 循环到 运行 它的每一行?我附上了我 table 的照片。我需要使用 altCount 和 refCount 列执行卡方检验。

   altCount refCount
     8        6
     3        7
     4        9 

我需要每一行的 p 值。我是 R 的新手。我想出了如何对单独的行执行卡方检验,但由于我有几千行,我需要一次循环到 运行 它。 我做到了:

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)
chisq.test(
     x = c,
     p = d, 
 )

但我不知道如何循环整个 table。你能详细解释一下怎么做吗?

enter image description here

对于初学者来说最简单的循环仍然是 for 循环:

d <- data.frame(a = c(8,3,4), b = c(6,7,9))
for(row in 1:nrow(d)){
    print(row)
    print(chisq.test(c(d[row,1],d[row,2])))
}

同样可以用

来完成
apply(d, 1, chisq.test)

后者更短,并为您提供了一个列表作为结果,这可能更适合进一步评估。