私は R の初心者です。プロジェクトの必要性により、10 万エントリの Chisq テストを行う必要があります。
私は数日間独学で学び、ループで chisq.test を実行するためのコードをいくつか書きました。コード:
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)
このコードにはいくつかの問題がある可能性があります。しかし、それは機能します。
ただし、実行速度は非常に遅いです。
「適用」を使用して改善しようとします
「for」を使用する代わりに、apply を 2 回使用する予定です
datarow= apply (the.data,1, matrix(the.data, byrow=T, nrow=3))
result=apply(datarow,1,chisq.test)
ただし、行列が関数ではないというエラーがあります。zsd chisq.test の出力はリストです。write.table を使用してデータを出力することはできません。
the.data はこんな感じです。
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
....
....
私は昼夜を問わず努力してきました。誰かが私を助けてくれることを願っています。どうもありがとうございました。