1

I have two matrices. I would like to apply a paired t test column by column and print the t-value, degrees of freedom, confidence interval and p value for each column. I started with the code below.

D1 and D2 are two matrices:

for (j in 1:n){
    t.test(D1[,j],D2[,j],paired=T)
}

Also, how can I print the each result from this loop?

joran
  • 169,992
  • 32
  • 429
  • 468
user1492427
  • 11
  • 1
  • 2

1 Answers1

7

Here's how I'd approach the problem:

#Make some random data
m1 <- matrix(rnorm(100), ncol = 5)
m2 <- matrix(rnorm(100), ncol = 5)

#Define a function to run your t.test, grab the relevant stats, and put them in a data.frame
f <- function(x,y){
  test <- t.test(x,y, paired=TRUE)
  out <- data.frame(stat = test$statistic,
                    df   = test$parameter,
                    pval = test$p.value,
                    conl = test$conf.int[1],
                    conh = test$conf.int[2]
                    )
  return(out)
}

#iterate over your columns via sapply
sapply(seq(ncol(m1)), function(x) f(m1[,x], m2[,x]))
#-----
     [,1]       [,2]       [,3]       [,4]       [,5]      
stat -0.7317108 1.73474    -0.0658436 0.6252509  -0.6161323
df   19         19         19         19         19        
pval 0.4732743  0.09898052 0.9481902  0.5392442  0.5451188 
conl -1.097654  -0.1259523 -0.7284456 -0.5680937 -0.7523431
conh 0.5289878  1.345625   0.6840117  1.052094   0.4101385

You may want to transpose the output since it is column major ordered.

Chase
  • 67,710
  • 18
  • 144
  • 161
  • Thank you, I have done as you suggested and I obtained such a table but my matrices are gene expression data in which genes are in rows and samples in columns. I want to extract genes with p-value > 0.05. But I don't know how to do that; Any help please? – Angel Jan 18 '19 at 12:57
  • @FereshTeh - been answered on SO many times over; check out [this question](https://stackoverflow.com/questions/1686569/filter-data-frame-rows-by-a-logical-condition) to start. – Chase Jan 18 '19 at 13:54