On 02/13/2013 08:48 PM, Ozgul Inceoglu wrote:
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?

Hi Ozgul,
As we don't know what the "ag" data frame looks like, I'll have to fake it. As your "bg" data frame is 6 rows with 17 columns, I'll make "ag" 6 rows with 3 columns. I'll also fake "bg" as I'm too lazy to write it all out. If "ag" differs in both rows and columns, you will have to pass use="pairwise.complete.obs" to the "cor.test" function.

bg<-data.frame(Otu00022=rnorm(6),Otu00039=rnorm(6),
 Otu00042=rnorm(6),Otu00101=rnorm(6))

ag<-data.frame(col1=rnorm(6),col2=rnorm(6),col3=rnorm(6))

allcor<-function(data1,data2) {
 corlist<-list()
 for(i in 1:length(data1)) {
  for(j in 1:length(data2))
   corlist[[length(data2)*(i-1)+j]]<-cor.test(data1[,i],data2[,j])
 }
 return(corlist)
}

Jim

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to