Function to convert a vector into a square matrix by filling up the lower triangular part of the matrix.

vec2mat(x, diag=FALSE, corr=!diag, dimnames)

Arguments

x

a vector of the correct length.

diag

logical to specify whether the vector also contains the diagonal values of the lower triangular part of the matrix (the default is FALSE).

corr

logical to specify whether the diagonal of the matrix should be replaced with 1's (the default is to do this when diag=FALSE).

dimnames

optional vector of the correct length with the dimension names of the matrix.

Details

The values in x are filled into the lower triangular part of a square matrix with the appropriate dimensions (which are determined based on the length of x). If diag=TRUE, then x is assumed to also contain the diagonal values of the lower triangular part of the matrix. If corr=TRUE, then the diagonal of the matrix is replaced with 1's.

Value

A matrix.

Examples

vec2mat(1:6, corr=FALSE)
#>      [,1] [,2] [,3] [,4]
#> [1,]   NA    1    2    3
#> [2,]    1   NA    4    5
#> [3,]    2    4   NA    6
#> [4,]    3    5    6   NA
vec2mat(seq(0.2, 0.7, by=0.1), corr=TRUE)
#>      [,1] [,2] [,3] [,4]
#> [1,]  1.0  0.2  0.3  0.4
#> [2,]  0.2  1.0  0.5  0.6
#> [3,]  0.3  0.5  1.0  0.7
#> [4,]  0.4  0.6  0.7  1.0
vec2mat(1:10, diag=TRUE)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    2    5    6    7
#> [3,]    3    6    8    9
#> [4,]    4    7    9   10
vec2mat(1:6, corr=FALSE, dimnames=c("A","B","C","D"))
#>    A  B  C  D
#> A NA  1  2  3
#> B  1 NA  4  5
#> C  2  4 NA  6
#> D  3  5  6 NA