I would like to convert a correlation matrix into a pairwise table (removing self-matches and duplicates).
Here is an example dataset.
rmat.test<-structure(c(1, 0.861194908618927, 0.826931774616241, 0.796892821788788,
0.83096307516098, 0.861194908618927, 1, 0.878752708435059, 0.855243384838104,
0.880544185638428, 0.826931774616241, 0.878752708435059, 1, 0.850607931613922,
0.850719928741455, 0.796892821788788, 0.855243384838104, 0.850607931613922,
1, 0.876053333282471, 0.83096307516098, 0.880544185638428, 0.850719928741455,
0.876053333282471, 1), .Dim = c(5L, 5L), .Dimnames = list(c("A",
"B", "C", "D", "E"), c("A", "B", "C", "D", "E")))
From answers from a previous post. I have the following code.
df.corr.pw<-reshape2::melt( cbind(
V1=rownames(rmat.test),
as.data.frame(rmat.test))
)
df.corr.pw<-subset(df.corr.pw,value!=1)
However, I can't figure out an efficient way to remove the duplicate entries (ie. row 2 for A-B, and row 6 for B-A).
> df.corr.pw
V1 variable value
2 B A 0.8611949
3 C A 0.8269318
4 D A 0.7968928
5 E A 0.8309631
6 A B 0.8611949
8 C B 0.8787527
9 D B 0.8552434
10 E B 0.8805442
11 A C 0.8269318
12 B C 0.8787527
14 D C 0.8506079
15 E C 0.8507199
16 A D 0.7968928
17 B D 0.8552434
18 C D 0.8506079
20 E D 0.8760533
21 A E 0.8309631
22 B E 0.8805442
23 C E 0.8507199
24 D E 0.8760533
I have tried this just using the upper.triangle, but I can't figure out how to retain and work withe the rownames.
rmat.up<-rmat.test[upper.tri(rmat.test)]
# below yields NULL
rownames(rmat.test[upper.tri(rmat.test)])
Thanks, any help appreciated.