Converting a nx1 matrix to a nxn square matrix

346 views Asked by At

I have asked a similar question before but this is slightly different compared to my previous question.

I have a matrix

   a  b  c  d  e
a  0  1  1  1  0
b  1  0  1  1  1

I am trying to convert this to a square matrix like this

   a  b  c  d  e
a  0  1  1  1  0
b  1  0  1  1  1
c  1  1  0  0  0
d  1  1  0  0  0
e  0  1  0  0  0

Any advise on how to do this in r will be helpful. Thanks in advance.

1

There are 1 answers

0
DatamineR On BEST ANSWER

What do you think about this solution?

res <- (merge(m, t(m)[(nrow(m)+1):ncol(m),], all = TRUE, by = 0:2))[,-1]
rownames(res) <- colnames(res)
res[is.na(res)] <- 0