R: t-test over all columns

后端 未结 5 783
醉话见心
醉话见心 2020-11-28 14:22

I tried to do t-test to all columns (two at a time) of my data frame, and extract only the p-value. Here is what I have come up with:

for (i in c(5:525) ) {
         


        
5条回答
  •  天涯浪人
    2020-11-28 14:35

    Try this one

    X <- rnorm(n=50, mean = 10, sd = 5)
    Y <- rnorm(n=50, mean = 15, sd = 6)
    Z <- rnorm(n=50, mean = 20, sd = 5)
    Data <- data.frame(X, Y, Z)
    
    library(plyr)
    
    combos <- combn(ncol(Data),2)
    
    adply(combos, 2, function(x) {
      test <- t.test(Data[, x[1]], Data[, x[2]])
    
      out <- data.frame("var1" = colnames(Data)[x[1]]
                        , "var2" = colnames(Data[x[2]])
                        , "t.value" = sprintf("%.3f", test$statistic)
                        ,  "df"= test$parameter
                        ,  "p.value" = sprintf("%.3f", test$p.value)
                        )
      return(out)
    
    })
    
    
    
      X1 var1  var2 t.value       df p.value
    1  1   X      Y  -5.598 92.74744   0.000
    2  2   X      Z  -9.361 90.12561   0.000
    3  3   Y      Z  -3.601 97.62511   0.000
    

提交回复
热议问题