Jinhyun Ju
Jinhyun Ju

Reputation: 75

R: Trying to convert a for loop using apply

I am trying to convert two for loop into one apply function with one for loop hoping that this will speed up my calculation a little bit. I know that using apply does not guarantee faster calculation but I wanted to give it a try (also for learning experience to get familiar with apply)

What I am trying to do is ;

Calculate pearson correlation coefficient for each row of two matrices and also get the p value.

Both of the matrices have the dimension of about 3000 X 100

Right now my code looks like this and it has been running for days...

cnt <- 1; 
res_row1 <- c();
res_row2 <- c();
res_corr <- c();
res_pval <- c();
for (i in (1:dim(m1)[1])) { 
  for (j in (1:dim(m2)[1])) { 
    c <- cor.test(as.matrix(m1[i,]), as.matrix(m2[j,])); 

    res_row1[cnt] <- rownames(m1)[i];
# need both row names in the output files 
    res_row2[cnt] <- rownames(m2)[j]; 

    res_corr[cnt] <- c$estimate; 
    res_pval[cnt] <- c$p.value;   
#   Storing the results for output

cnt<-cnt+1; 


  }
  comp <- (i / dim(m1[1]) * 100; 
  cat(sprintf("Row number of file 1 = %f | %f percent complete \n", i, comp))

}
results <- cbind(res_row1, res_row2, res_corr, res_pval)

Can you guys help me out ?

Upvotes: 1

Views: 695

Answers (1)

liuminzhao
liuminzhao

Reputation: 2455

Take a look at the manual of cor:

If 'x' and 'y' are matrices then the covariances (or correlations) between the columns of 'x' and the columns of 'y' are computed.

So, I would like to try:

cor(t(m1), t(m2))

For p-values, try to use double apply function:

R > x <- matrix(rnorm(12), 4, 3)
R > y <- matrix(rnorm(12), 4, 3)
R > cor(t(x), t(y))
        [,1]    [,2]    [,3]     [,4]
[1,]  0.9364  0.8474 -0.7131  0.67342
[2,] -0.9539 -0.9946  0.9936 -0.07541
[3,]  0.8013  0.9046 -0.9752 -0.25822
[4,]  0.3767  0.5541 -0.7205 -0.72040
R > t(apply(x, 1, function(a) apply(y, 1, function(b) cor(b, a))))
        [,1]    [,2]    [,3]     [,4]
[1,]  0.9364  0.8474 -0.7131  0.67342
[2,] -0.9539 -0.9946  0.9936 -0.07541
[3,]  0.8013  0.9046 -0.9752 -0.25822
[4,]  0.3767  0.5541 -0.7205 -0.72040
R > t(apply(x, 1, function(a) apply(y, 1, function(b) cor.test(b, a)$p.value)))
       [,1]    [,2]    [,3]   [,4]
[1,] 0.2283 0.35628 0.49461 0.5297
[2,] 0.1940 0.06602 0.07231 0.9519
[3,] 0.4083 0.28034 0.14201 0.8337
[4,] 0.7541 0.62615 0.48782 0.4879
R > cor.test(x[1,], y[1,])$p.value
[1] 0.2283
R > cor.test(x[1,], y[2,])$p.value
[1] 0.3563

Upvotes: 1

Related Questions