Question: Correlation Of Correlations In R
1
gravatar for deschisandy
6.8 years ago by
deschisandy60
deschisandy60 wrote:

I have two gene correlation matrices A and B,

 seed(1) 
  X <- data.frame(matrix(rnorm(2000), nrow=10))
  Y <- data.frame(matrix(rnorm(2000), nrow=10))

I would like to find the correlation between each row of A and B in the following way. For example there should be a correlation value for row 1 of X and row 1 of Y.

Similarly applying for all rows there will be in total ten values ( because there are ten rows)

R correlation • 3.7k views
ADD COMMENTlink modified 6.8 years ago by zx87549.3k • written 6.8 years ago by deschisandy60
1
gravatar for zx8754
6.8 years ago by
zx87549.3k
London
zx87549.3k wrote:

Try this:

X <- matrix(rnorm(2000), nrow=10)
Y <- matrix(rnorm(2000), nrow=10)

apply(matrix(1:10),1,
      function(rowID){
        cor.test(X[rowID,],Y[rowID,])$estimate
        })
ADD COMMENTlink written 6.8 years ago by zx87549.3k
2

You should really use sapply if you want to do it this way, i.e. sapply(1:10, function(row) cor(X[row,], Y[row,]))

edit: a more satisfying method is diag(cor(t(X), t(Y))) (I'll admit I had to google for this in the end)

ADD REPLYlink modified 6.8 years ago • written 6.8 years ago by Ben2.0k
2

Yes, sapply looks neater, still learning R way looping - apply...

ADD REPLYlink written 6.8 years ago by zx87549.3k

Thanks , It works. Is it possible to print the row-ids as well along with the values? And also could you please explain how this was done ?

ADD REPLYlink modified 6.8 years ago • written 6.8 years ago by deschisandy60
1

There are great guides on R at stackoverflow.

ADD REPLYlink written 6.8 years ago by zx87549.3k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1645 users visited in the last hour