1

I am interested to know what a proper x (vector matrix or data frame) input looks like. I am currently using the function in two different sorts of matrices. However, I am not sure how R would interpret my data the way I intend. I will explain the types of matrix by example.

Type 1

           Gene1 Gene2 Gene3
    sample1
    sample2

Type 2

          Sample1 Sample2 Sample3
gene 1
gene 2
gene 3

Are either of these formats valid x parameters? I input both of types of matrices and get some results, but without knowing whether or not this a proper use the function, these are just random numbers. Thank you for your time. I apologize that this isn't more interesting.

0

1 Answer 1

9

When X is a matrix, cor(X) will produce a square correlation matrix with the number of rows and columns equal to the number of columns in the original matrix. In other words, cor produces correlations between the columns in the matrix. Here is a simple example:

> x <- rnorm(5)
> y <- rnorm(5)
> cbind(x,y)
            x        y
[1,]  1.67287  1.70663
[2,] -1.23120  0.56948
[3,]  0.67538 -0.20596
[4,] -1.21077  0.11648
[5,]  0.60409  1.15405

> cor(cbind(x,y))
        x       y
x 1.00000 0.56329
y 0.56329 1.00000

Does that answer your question?

4
  • I think you know what I want to know. Is it necessary for the columns to be variables and the rows samples for this to yield correct data. Essentially would it make sense if you used an rbind() rather than a cbind() or would this cause the output to be garbage?
    – order
    Jun 24, 2012 at 4:14
  • 1
    It doesn't really matter how you put your data together - it should be clear to you due to the documentation and Jason's example that it will construct a correlation matrix of the correlation between the columns of the data.
    – Dason
    Jun 24, 2012 at 4:20
  • @Dason I read the documentation and did not gather that the comparison was done column-wise; I can see now that is clearly stated in the documentation. I'm sure you have at some point failed to absorb a piece of documentation.
    – order
    Jun 24, 2012 at 4:36
  • 1
    @order Try out rbind; it should return, in the case of the example above, a 5-by-5 matrix of 1 and -1. Certainly not what you are looking for. Jun 24, 2012 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.