简体   繁体   中英

R - converting correlation matrix to pairwise

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 AB, and row 6 for BA).

> 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.

You could try (without using your function)

rmat.test[lower.tri(rmat.test,diag=TRUE)]=NA # put NA
rmat.test<-as.data.frame(as.table(rmat.test)) # as a dataframe
rmat.test<-na.omit(rmat.test) # remove NA
rmat.test<-rmat.test[with(rmat.test, order(-Freq)), ] # order by correlation

rmat.test
   Var1 Var2      Freq
22    B    E 0.8805442
12    B    C 0.8787527
24    D    E 0.8760533
6     A    B 0.8611949
17    B    D 0.8552434
23    C    E 0.8507199
18    C    D 0.8506079
21    A    E 0.8309631
11    A    C 0.8269318
16    A    D 0.7968928

and another way:

tmp <- melt(rmat.test) 
tmp <- data.frame(t(apply(tmp, 1, sort)))
tmp <- tmp[duplicated(tmp[, 1 : 2], MARGIN = 1), ]
tmp[, 3 : 1]

  # X3 X2        X1
#6   B  A 0.8611949
#11  C  A 0.8269318
#12  C  B 0.8787527
#16  D  A 0.7968928
#17  D  B 0.8552434
#18  D  C 0.8506079
#21  E  A 0.8309631
#22  E  B 0.8805442
#23  E  C 0.8507199
#24  E  D 0.8760533

you first melt, then sort the letter combinations to pick out duplicated pairs later with duplicated .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM