Similar questions have been asked, however, none have the additional element of having to split the vector names, so I am asking a new question.
I am trying to turn a named vector into a symmetric matrix in R. My vector contains the names of every combination of values in the matrix. so I need to split the names into their component parts.
For example, if my data looks like this:
v <- c(
"x1 x2" = 0.81899860,
"x1 x3" = 0.10764701,
"x2 x3" = 0.03923967,
"x1 x4" = 0.03457240,
"x2 x4" = 0.05954789,
"x3 x4" = 0.15535316,
"x1 x5" = 0.04041266,
"x2 x5" = 0.05421003,
"x3 x5" = 0.09198977,
"x4 x5" = 0.15301872
)
We can see that each name is a combination of 2 of the variables. I am trying to turn this into a symmetric matrix (with the diagonal as zeros). For clarity, my desired output would look like:
x1 x2 x3 x4 x5
x1 0.00000000 0.81899860 0.10764701 0.03457240 0.04041266
x2 0.81899860 0.00000000 0.03923967 0.05954789 0.05421003
x3 0.10764701 0.03923967 0.00000000 0.15535316 0.09198977
x4 0.03457240 0.05954789 0.15535316 0.00000000 0.15301872
x5 0.04041266 0.05421003 0.09198977 0.15301872 0.00000000
Any suggestions as to how I could do this?
EDIT
As one of the answers highlighted that my question was too vague, im making an edit to reflect this. I am looking for a general solution to this problem, no matter what the names are in the vector. For example, my named vector could look like this:
v <- c(
"apple banana" = 0.81899860,
"apple orange" = 0.10764701,
"banana orange" = 0.03923967,
"apple pear" = 0.03457240,
"banana pear" = 0.05954789,
"orange pear" = 0.15535316,
"apple plum" = 0.04041266,
"banana plum" = 0.05421003,
"orange plum" = 0.09198977,
"pear plum" = 0.15301872
)