1

I have a dataframe as follows:

x <- data.frame(Name=c("a", "b","c", "d", "e"),A=(1:5), B=(2:6), C=(7:11), D=c(1,1,1,1,1))

I want to get a dataframe including all the pearson coefficients of a vs b, a vs c, a vs d, a vs e, b vs a, b vs c, b vs d, b vs e....etc (excluding self-comparisons, i.e. a-a, b-b, etc).

In order words, I want to change the following dataframe

            A   B   C   D
a   1   2   7   1
b   2   3   8   1
c   3   4   9   1
d   4   5   10  1
e   5   6   11  1

To a result dataframe like this

a   b   0.989143436
a   c   0.963282002
a   d   0.93048421
a   e   0.89585579
b   c   0.9922798
b   d   0.974216034
b   e   0.951427811
c   d   0.994675789
c   e   0.982264673
d   e   0.996357429

Pls kindly instruct efficient way of doing this.

EDIT

Thanks for Michaelv2's help.

Based on the suggested code, I found the results is as follows:

   X1 X2 value  
1   A  A     1   
2   B  A     1   
3   C  A     1  
4   D  A    NA  
5   A  B     1  
6   B  B     1  
7   C  B     1  
8   D  B    NA  
9   A  C     1  
10  B  C     1  
11  C  C     1  
12  D  C    NA  
13  A  D    NA  
14  B  D    NA  
15  C  D    NA  
16  D  D     1  

The error message is " Warning message: In cor(x[2:5], method = "pearson") : the standard deviation is zero"

It seems to me that I may have misused the code, could you pls kindly further instruct how to solve this problem further? Thank you.

mbq
  • 18,510
  • 6
  • 49
  • 72
a83
  • 11
  • 2

1 Answers1

3

You could use something like the following:

require(reshape)

y <- as.data.frame(t(x[2:5]), stringsAsFactors=FALSE)
colnames(y) <- x[[1]]

yrho <- melt(cor(y, method="pearson"))
subset(yrho, yrho$X1 != yrho$X2)

Result:

    X1 X2     value
2   b  a 0.9891434
3   c  a 0.9632820
4   d  a 0.9304842
5   e  a 0.8958558
6   a  b 0.9891434
8   c  b 0.9922798
9   d  b 0.9742160
10  e  b 0.9514278
11  a  c 0.9632820
12  b  c 0.9922798
14  d  c 0.9946758
15  e  c 0.9822647
16  a  d 0.9304842
17  b  d 0.9742160
18  c  d 0.9946758
20  e  d 0.9963574
21  a  e 0.8958558
22  b  e 0.9514278
23  c  e 0.9822647
24  d  e 0.9963574
michaelv2
  • 306
  • 1
  • 4
  • 1
    Well, row labels might be row labels, there is no need to omit them then. Plus, the OP wanted row-row correlations, not column-column (but a single `t` will do the job). – mbq Jun 13 '11 at 07:55
  • Technically I omitted the first column of the data frame (a character or factor vector, depending on your settings), not row labels, but you're quite right about the row-wise correlations. Thanks for pointing that out. – michaelv2 Jun 13 '11 at 10:15