【问题标题】:What is the simplest way to complete a function on every row of a large table?在大表的每一行上完成函数的最简单方法是什么?
【发布时间】:2021-01-20 17:19:16
【问题描述】:

所以我想对 3000+ 行表的每一行进行 Fisher 精确测试(单边),其格式与以下示例匹配

gene sample_alt sample_ref population_alt population_ref
One 4 556 770 37000
Two 5 555 771 36999
Three 6 554 772 36998

理想情况下,我想让表格的另一列相当于

[(4+556)!(4+770)!(770+37000)!(556+37000)!]/[4!(556!)770!(37000!)(4+556+770+ 37000)!]

第一行数据,以此类推,以此类推。

我知道如何在 R 中对简单的 2x2 表进行 Fisher 测试,但我不知道如何将 Fisher.test() 函数应用于大表的每一行。我也不能使用 excel 公式,因为阶乘数字变得如此之大,以至于它们达到了 excel 的位数限制并导致 #NUM 错误。简单地完成此操作的最佳方法是什么?提前致谢!

【问题讨论】:

    标签: statistics genetics


    【解决方案1】:

    从桌面上以制表符分隔的文本文件 (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()
    
    

    【讨论】:

      猜你喜欢
      • 2020-12-29
      • 2016-09-05
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2011-01-11
      • 2022-08-13
      • 2010-09-23
      相关资源
      最近更新 更多