在大 table 的每一行上完成功能的最简单方法是什么?
What is the simplest way to complete a function on every row of a large table?
所以我想对 3000 多行的每一行进行 Fisher 精确测试(单面)table,其格式与以下示例相匹配
gene
sample_alt
sample_ref
population_alt
population_ref
One
4
556
770
37000
Two
5
555
771
36999
Three
6
554
772
36998
理想情况下,我希望 table 的另一列等同于
[(4+556)!(4+770)!(770+37000)!(556+37000)!]/[4!(556!)770!(37000!)(4+556+ 770+37000)!]
为第一行数据,以此类推table的每一行。
我知道如何在 R 中对简单的 2x2 tables 进行费舍尔测试,但我不知道如何将 fisher.test() 函数应用于大数据的每一行table。我也不能使用 excel 公式,因为数字随着阶乘变得太大,以至于它们达到 excel 的数字限制并导致 #NUM 错误。简单完成此操作的最佳方法是什么?提前致谢!
从桌面上的制表符分隔文本文件 (table.txt) 开始,其格式与题干问题中显示的格式相同
if(!require(psych)){install.packages("psych")}
multiFisher = function(file="Desktop/table.txt", saveit=TRUE,
outfile="Desktop/table.csv", progress=T,
verbose=FALSE, digits=3, ... )
{
require(psych)
Data = read.table(file, skip=1, header=F,
col.names=c("Gene", "MD", "WTD", "MC", "WTC"), ...)
if(verbose){print(str(Data))}
Data$Fisher.p = NA
Data$phi = NA
Data$OR1 = format(0.123, nsmall=3)
Data$OR2 = NA
if(progress){cat("\n")}
for(i in 1:length(Data$Gene)){
Matrix = matrix(c(Data$WTC[i],Data$MC[i],Data$WTD[i],Data$MD[i]), nrow=2)
Fisher = fisher.test(Matrix, alternative = 'greater')
Data$Fisher.p[i] = signif(Fisher$p.value, digits=digits)
Data$phi[i] = phi(Matrix, digits=digits)
OR1 = (Data$WTC[i]*Data$MD[i])/(Data$MC[i]*Data$WTD[i])
OR2 = 1 / OR1
Data$OR1[i] = format(signif(OR1, digits=digits), nsmall=3)
Data$OR2[i] = signif(OR2, digits=digits)
if(progress) {cat(".")}
}
if(progress){cat("\n"); cat("\n")}
if(saveit){write.csv(Data, outfile)}
return(Data)
}
multiFisher()
所以我想对 3000 多行的每一行进行 Fisher 精确测试(单面)table,其格式与以下示例相匹配
gene | sample_alt | sample_ref | population_alt | population_ref |
---|---|---|---|---|
One | 4 | 556 | 770 | 37000 |
Two | 5 | 555 | 771 | 36999 |
Three | 6 | 554 | 772 | 36998 |
理想情况下,我希望 table 的另一列等同于
[(4+556)!(4+770)!(770+37000)!(556+37000)!]/[4!(556!)770!(37000!)(4+556+ 770+37000)!]
为第一行数据,以此类推table的每一行。
我知道如何在 R 中对简单的 2x2 tables 进行费舍尔测试,但我不知道如何将 fisher.test() 函数应用于大数据的每一行table。我也不能使用 excel 公式,因为数字随着阶乘变得太大,以至于它们达到 excel 的数字限制并导致 #NUM 错误。简单完成此操作的最佳方法是什么?提前致谢!
从桌面上的制表符分隔文本文件 (table.txt) 开始,其格式与题干问题中显示的格式相同
if(!require(psych)){install.packages("psych")}
multiFisher = function(file="Desktop/table.txt", saveit=TRUE,
outfile="Desktop/table.csv", progress=T,
verbose=FALSE, digits=3, ... )
{
require(psych)
Data = read.table(file, skip=1, header=F,
col.names=c("Gene", "MD", "WTD", "MC", "WTC"), ...)
if(verbose){print(str(Data))}
Data$Fisher.p = NA
Data$phi = NA
Data$OR1 = format(0.123, nsmall=3)
Data$OR2 = NA
if(progress){cat("\n")}
for(i in 1:length(Data$Gene)){
Matrix = matrix(c(Data$WTC[i],Data$MC[i],Data$WTD[i],Data$MD[i]), nrow=2)
Fisher = fisher.test(Matrix, alternative = 'greater')
Data$Fisher.p[i] = signif(Fisher$p.value, digits=digits)
Data$phi[i] = phi(Matrix, digits=digits)
OR1 = (Data$WTC[i]*Data$MD[i])/(Data$MC[i]*Data$WTD[i])
OR2 = 1 / OR1
Data$OR1[i] = format(signif(OR1, digits=digits), nsmall=3)
Data$OR2[i] = signif(OR2, digits=digits)
if(progress) {cat(".")}
}
if(progress){cat("\n"); cat("\n")}
if(saveit){write.csv(Data, outfile)}
return(Data)
}
multiFisher()