[R] spearman correlation and p-value as a matrix

Ozgul Inceoglu Ozgul.Inceoglu at ulb.ac.be
Wed Feb 13 10:48:46 CET 2013


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.
> bg <- read.table (file.choose(), header=T, row.names) 
> bg 
> Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 
> Gi20Jun11 0.001217 0 0.001217 0 0.000000 0 0 0 0.001217 0 0 0 0 0 0.001217 0 0.001217 
> Gi40Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 
> Gi425Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 
> Gi45Jun11 0.000000 0 0.000000 0 0.001513 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 
> Gi475Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 
> Gi50Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 
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



More information about the R-help mailing list