1

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.

Tyrone Williams
  • 77
  • 1
  • 10
  • @RStudent thanks for helping. I found the answer, http://stackoverflow.com/questions/21419507/adjacency-matrix-in-r?rq=1 – bison2178 Nov 13 '14 at 22:57

1 Answers1

3

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
DatamineR
  • 10,428
  • 3
  • 25
  • 45