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

查看:90
本文介绍了如何使用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.

但是,它运行非常慢.

我尝试通过使用应用"来改善它

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.

推荐答案

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

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天全站免登陆