When you work with correlation matrices, you are probably not interested in entering the diagonal, as well as the upper and lower parts. You can manipulate / extract these three parts separately using diag()
, upper.tri()
and lower.tri()
.
> M <- diag(3) # create 3x3 matrix, diagonal defaults to 1's > M[lower.tri(M, diag=F)] <- c(0.1, 0.5, 0.9) # read in lower part > M # lower matrix containing all information [,1] [,2] [,3] [1,] 1.0 0.0 0 [2,] 0.1 1.0 0 [3,] 0.5 0.9 1
If you want to get the full matrix:
> M[upper.tri(M, diag=F)] <- M[lower.tri(M)] # fill upper part > M # full matrix [,1] [,2] [,3] [1,] 1.0 0.1 0.5 [2,] 0.1 1.0 0.9 [3,] 0.5 0.9 1.0
source share