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.