Skip to content
Prev 317440 / 398503 Next

spearman correlation and p-value as a matrix

I have two data matrices that I want to make the correlation between each column from data1 and each column from data 2 and also calculate the p-value Matrices dont have the same size and I tried such a script.
ag <- read.table (file.choose(), header=T, row.names) 

for (i in 1:(ncol(bg))) 
for (j in 1:(ncol(ag))) 
print(c(i,j))
final_matrix <- matrix(rep("0",ncol(bg)*ncol(ag)),ncol=ncol(bg),nrow=ncol(ag))

cor <- cor.test(as.vector(as.matrix(bg[,i])),as.vector(as.matrix(ag[,j])), method="spearman")

#but the output is not matrice with all the values but a single correlation value

data:  bg[, i] and ag[, j] 
t = 2.2992, df = 26, p-value = 0.02978
alternative hypothesis: true correlation is not equal to 0 
95 percent confidence interval:
 0.04485289 0.67986803 
sample estimates:
      cor 
0.4110515 

# How I can creat an outfile with all the correlations and p-values?

Thank you very much!
?zg?l