如何使用 apply 在循环中运行 chisq.test [英] how to run chisq.test in loops using apply

查看:23
本文介绍了如何使用 apply 在循环中运行 chisq.test的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 的新手.由于我的项目需要,我需要对十万个条目进行Chisq测试.

I am a newbie of R. Due to the need of my project, I need to do Chisq test for hundred thousand entries.

自学了几天,写了一些代码循环运行chisq.test.代码:

I learned by myself for a few days and write some code for runing chisq.test in loops. codes:

the.data = read.table ("test_chisq_allelefrq.txt", header=T, sep="\t",row.names=1)
p=c()
ID=c()
for (i in 1:nrow(the.data)) {
data.row = the.data [i,]
data.matrix = matrix ( c(data.row$cohort_1_AA, data.row$cohort_1_AB,       data.row$cohort_1_BB, data.row$cohort_2_AA, data.row$cohort_2_AB, data.row$cohort_2_BB,data.row$cohort_3_AA,data.row$cohort_3_AB,data.row$cohort_3_BB), byrow=T, nrow=3)
chisq = chisq.test(data.matrix)
pvalue=chisq$p.value
p=c(p, pvalue)
No=row.names(the.data)[i]
ID=c(rsid, SNP )
}
results=data.frame(ID,p)
write.table (results,  file = "chisq-test_output.txt", append=F, quote = F, sep = "\t ",eol = "\n", na = "NA", dec = ".", row.names = F, col.names = T) 

这段代码可能有几个问题.但它有效.

this code might have several problems. but it works.

但是,它运行得很慢.

我尝试使用apply"来改进它

I try to improve it by using "apply"

我打算使用两次 apply 而不是使用for"

I plan to use apply twice instead of using "for"

datarow= apply (the.data,1,  matrix(the.data, byrow=T, nrow=3))
result=apply(datarow,1,chisq.test)

但是,有错误说矩阵不是函数.zsd chisq.test 输出是一个列表,我不能用write.table输出数据.

However, there is error saying matrix is not a function. zsd the chisq.test output is a list, I cannot use write.table to output the data.

.data 是这样的.

the.data is like this.

SN0001 and 9 numbers
           cohort_1_AA cohort_1_AB cohort_1_BB cohort_2_AA cohort_2_AB cohort_2_BB cohort_3_AA cohort_3_AB cohort_3_BB
SN0001     197         964        1088       877      858      168     351    435      20
....
....

我已经尝试了几天几夜.希望可以有人帮帮我.非常感谢.

I have been trying for days and nights. Hope someone can help me. Thank you very much.

推荐答案

要使用应用函数组,首先定义我们自己的函数然后应用它很容易.让我们这样做.

To use apply group of functions it is easy first to define our own function and then apply it. Lets do that.

    ##first define the function to apply
    Chsq <- function(x){
   ## input is a row of your data
   ## creating a table from each row
         x <- matrix(x,byrow =TRUE,nrow=3)
    ### this will return the p value
      return(chisq.test(x)$p.value)
    }
## Now apply this function
data = read.table ("test_chisq_allelefrq.txt", header=T, sep="\t",row.names=1)
## by using as.vector convert the output into a vector
P_Values <- as.vector(apply(data,1,Chsq))
result <- cbind(rownames(data),P_Values)
write.table (results,  file = "chisq-test_output.txt", append=F, quote = F, sep = "\t ",eol = "\n", na = "NA", dec = ".", row.names = F, col.names = T) 

试试这个代码希望它有效!!:) 如果答案对您有用,请接受它为正确答案.谢谢

Try this code hopefully it works !! :) Accept the answer as correct if it works for you. thanks

这篇关于如何使用 apply 在循环中运行 chisq.test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆