简体   繁体   中英

transform pair correlation to correlation matrix

I am stuck at transforming pair correlation to matrix correlation. If the correlation matrix is contained in the pair correlation, copy the pair correlation to the matrix, if not, fill it with 0.

library("stringr")
cor<- data.frame("Var1"=str_dup(letters[1:10], 10) , "Var2"=str_dup(letters[11:20], 10), "b"=runif(20, min=1, max=30))

m <- matrix(NA, 23, 27)
row.names(m)<- c(cor$Var1, "sfrwer", "dfser", "kdf1")
colnames(m)<- c(cor$Var2, "sfrwer1", "dfser2", "kdf3", "dflkdfl", "kldfa", "aerfkaj", "ajdfha")

for (i in 1:nrow(m)) {
  for (j in 1:ncol(m)) {
if (m[i, 1]= cor$Var2 && m[1, j]=cor$Var1) {
  m[i, j] <- cor$b
} else {
  m[i, j] <- 0
}
}

Perhaps we need xtabs if the intention is to reshape the 'long' format into a matrix format

xtabs(b ~ Var1 + Var2, cor)

-output

           Var2
Var1         kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr ssssssssss tttttttttt
  aaaaaaaaaa   16.31062    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  bbbbbbbbbb    0.00000   16.44782    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  cccccccccc    0.00000    0.00000   20.21136    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  dddddddddd    0.00000    0.00000    0.00000   18.47966    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  eeeeeeeeee    0.00000    0.00000    0.00000    0.00000   30.75083    0.00000    0.00000    0.00000    0.00000    0.00000
  ffffffffff    0.00000    0.00000    0.00000    0.00000    0.00000   41.20744    0.00000    0.00000    0.00000    0.00000
  gggggggggg    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   38.55080    0.00000    0.00000    0.00000
  hhhhhhhhhh    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   20.69509    0.00000    0.00000
  iiiiiiiiii    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   16.79159    0.00000
  jjjjjjjjjj    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   19.11196

If we need all the levels to make it symmetric, convert the columns to factor with levels specified as the unique elements in both the 'Var1', 'Var2' and then do the xtabs

un1 <- sort(unique(unlist(cor[c("Var1", "Var2")])))
xtabs(b ~ Var1 + Var2, transform(cor, Var1 = factor(Var1, 
           levels = un1), Var2 = factor(Var2, levels = un1)))

-output

            Var2
Var1         aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii jjjjjjjjjj kkkkkkkkkk llllllllll
  aaaaaaaaaa    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   16.31062    0.00000
  bbbbbbbbbb    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   16.44782
  cccccccccc    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  dddddddddd    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  eeeeeeeeee    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  ffffffffff    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  gggggggggg    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  hhhhhhhhhh    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  iiiiiiiiii    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  jjjjjjjjjj    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  kkkkkkkkkk    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  llllllllll    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  mmmmmmmmmm    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  nnnnnnnnnn    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  oooooooooo    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  pppppppppp    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  qqqqqqqqqq    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  rrrrrrrrrr    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  ssssssssss    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  tttttttttt    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
            Var2
Var1         mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr ssssssssss tttttttttt
  aaaaaaaaaa    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  bbbbbbbbbb    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  cccccccccc   20.21136    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  dddddddddd    0.00000   18.47966    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  eeeeeeeeee    0.00000    0.00000   30.75083    0.00000    0.00000    0.00000    0.00000    0.00000
  ffffffffff    0.00000    0.00000    0.00000   41.20744    0.00000    0.00000    0.00000    0.00000
  gggggggggg    0.00000    0.00000    0.00000    0.00000   38.55080    0.00000    0.00000    0.00000
  hhhhhhhhhh    0.00000    0.00000    0.00000    0.00000    0.00000   20.69509    0.00000    0.00000
  iiiiiiiiii    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   16.79159    0.00000
  jjjjjjjjjj    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000   19.11196
  kkkkkkkkkk    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  llllllllll    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  mmmmmmmmmm    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  nnnnnnnnnn    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  oooooooooo    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  pppppppppp    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  qqqqqqqqqq    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  rrrrrrrrrr    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  ssssssssss    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
  tttttttttt    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000

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