0

I have a matrix of characters:

mat1
    [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "0"  "B"  "A"  "C"  "D"  "D" 
[2,] "0"  "0"  "B"  "C"  "C"  "C" 
[3,] "0"  "0"  "0"  "D"  "D"  "C" 
[4,] "0"  "0"  "0"  "0"  "B"  "B" 
[5,] "0"  "0"  "0"  "0"  "0"  "A" 
[6,] "0"  "0"  "0"  "0"  "0"  "0" 

I want to have a Symmetrical matrix of that, as below:

    [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "0"  "B"  "A"  "C"  "D"  "D" 
[2,] "B"  "0"  "B"  "C"  "C"  "C" 
[3,] "A"  "B"  "0"  "D"  "D"  "C" 
[4,] "C"  "C"  "D"  "0"  "B"  "B" 
[5,] "D"  "C"  "D"  "B"  "0"  "A" 
[6,] "D"  "C"  "C"  "B"  "A"  "0"
SA12
  • 318
  • 2
  • 10

1 Answers1

1

You can set the lower triangular part of the matrix as equal to the lower triangular part of the transposed matrix, by using the lower.tri functions on the matrix mat1:

mat1[lower.tri(mat1)] <- t(mat1)[lower.tri(mat1)]
user2474226
  • 1,472
  • 1
  • 9
  • 9
  • you're right, my mistake. pls see corrected. this question is a duplicate, for example, of [this one](https://stackoverflow.com/questions/18165320/creating-a-symmetric-matrix-in-r). – user2474226 Jul 01 '20 at 15:35