Reputation: 183
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.
cor<- data.frame("var1"=paste0("aa", sep="_", 1:13), "var2"=paste0("bb", sep="_", 1:17), "b"=runif(221, min=0, max=1))
m <- matrix(0, 23, 27)
rownames(m)<- c(unique(cor$var1), paste0("aa", sep="_", 14:23))
colnames(m)<- c(unique(cor$var2), paste0("bb", sep="_", 18:27))
for (i in 1:nrow(m)) {
itemrow<- row.names(m)[i]
for (j in 1:ncol(m)) {
itemcol<- colnames(m)[j]
if (itemrow== cor$var2 && itemcol==cor$var1) {
m[itemrow, itemcol] <- cor$b
} else {
NULL
}
}
}
Upvotes: 1
Views: 309
Reputation: 887891
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
Or if we want to do the assignment on the created matrix
m[as.matrix(cor[1:2])] <- cor$b
Upvotes: 2