2012-10-31

R: Function to perform t-test on all possible combinations of columns in a dataframe



Sometimes I need to perform a t-test on all possible combinations of the columns of a data-frame and rather than computing them all one by one, I wrote this small function:


ttest_all_possible  <- function(dataframe) {
    nr_columns = dim(dataframe)[2]
    results <- matrix(NA, nr_columns, nr_columns)
    for (i in seq(1, nr_columns)){
        for (j in seq(1, nr_columns)){
            results[i, j] <- t.test(dataframe[,i], dataframe[,j])$p.value
        }
    }
    return(results)
}


Works fine:
> test <- cbind(rnorm(10),rnorm(10), rnorm(10))
> test
             [,1]       [,2]       [,3]
 [1,]  0.64061950 -0.5643179  0.7450485
 [2,]  1.90703328 -0.8000197  0.2444611
 [3,]  0.96346636 -0.6278075  0.1283072
 [4,] -0.25758024 -0.9657888 -0.8832595
 [5,]  0.36934381  1.4149775  0.8837320
 [6,]  0.60584130  0.2556500 -0.2437533
 [7,] -0.24559562 -0.7774171  0.1279253
 [8,] -1.53276425  0.3557177  0.1611155
 [9,] -1.55474156  0.3488891  0.1025763
[10,] -0.08366046 -0.2823561  1.3618903
> ttest_all_possible(test)
          [,1]      [,2]      [,3]
[1,] 1.0000000 0.5603004 0.6495045
[2,] 0.5603004 1.0000000 0.1817692
[3,] 0.6495045 0.1817692 1.0000000

No comments:

Post a Comment