I have a matrix (1580 x 37), I need to compute cor.test between each row.
n = nrow(filtered)
c =ncol(filtered)
cor.table = NULL
x = NULL
y = NULL
cor.val = NULL
cor.sig = NULL
library(psych)
for (i in 1 : (n-1)) {
x_name = rownames(filtered)[i]
x_exps = filtered[i, ]
for (j in (i+1) : n) {
y_name = rownames(filtered)[j]
y_exps = filtered[j, ]
output = cor.test(x_exps,y_exps)
x = c(x, x_name)
y = c(y, y_name)
cor.val = c(cor.val, output$estimate)
cor.sig = c(cor.sig, output$p.value)
}
}
adjusted_p_cor <- p.adjust(cor.sig, method = "fdr", n = length(cor.sig))
cor.table = data.frame (x, y, cor.val, adjusted_p_cor)
For a small matrix the code works fine and return me correct results:
x y cor.val adjusted_p_cor
sample_1 Sample_2 -0.46 0.008
sample_1 Sample_3 0.82 5.50
and so on ....
For a big matrix, calculation is slow and R stuck after several hours.
I will appreciate if anyone let me know alternative solutions.