Skip to contents

[Stable]

These functions help users to make upper, lower, or symmetric matrices easily.

Usage

make_upper_tri(x, diag = NA)

make_lower_tri(x, diag = NA)

make_lower_upper(lower, upper, diag = NA)

make_sym(x, make = "upper", diag = NA)

tidy_sym(x, keep_diag = TRUE)

Arguments

x

A matrix to apply the function. It must be a symmetric (square) matrix in make_upper_tri() and make_lower_tri() or a triangular matrix in make_sym(). tidy_sym() accepts both symmetrical or triangular matrices.

diag

What show in the diagonal of the matrix. Default to NA.

lower

A square matrix to fill the lower diagonal of the new matrix.

upper

A square matrix to fill the upper diagonal of the new matrix.

make

The triangular to built. Default is "upper". In this case, a symmetric matrix will be built based on the values of a lower triangular matrix.

keep_diag

Keep diagonal values in the tidy data frame? Defaults to TRUE.

Value

An upper, lower, or symmetric matrix, or a tidy data frame.

Details

  • make_upper_tri() makes an upper triangular matrix using a symmetric matrix.

  • make_lower_tri() makes a lower triangular matrix using a symmetric matrix.

  • make_sym() makes a lower triangular matrix using a symmetric matrix.

  • tidy_sym() transform a symmetric matrix into tidy data frame.

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

# \donttest{
library(metan)
m <- cor(select_cols(data_ge2, 5:10))
make_upper_tri(m)
#>    EH       EP        EL        ED        CL        CD
#> EH NA 0.869546 0.3626537 0.6302561 0.3971935 0.2805118
#> EP NA       NA 0.2634237 0.4580196 0.3908239 0.1750448
#> EL NA       NA        NA 0.3851451 0.2554068 0.9118653
#> ED NA       NA        NA        NA 0.6974629 0.3897128
#> CL NA       NA        NA        NA        NA 0.3003636
#> CD NA       NA        NA        NA        NA        NA
make_lower_tri(m)
#>           EH        EP        EL        ED        CL CD
#> EH        NA        NA        NA        NA        NA NA
#> EP 0.8695460        NA        NA        NA        NA NA
#> EL 0.3626537 0.2634237        NA        NA        NA NA
#> ED 0.6302561 0.4580196 0.3851451        NA        NA NA
#> CL 0.3971935 0.3908239 0.2554068 0.6974629        NA NA
#> CD 0.2805118 0.1750448 0.9118653 0.3897128 0.3003636 NA
make_lower_tri(m) %>%
make_sym(diag = 0)
#>           EH        EP        EL        ED        CL        CD
#> EH 0.0000000 0.8695460 0.3626537 0.6302561 0.3971935 0.2805118
#> EP 0.8695460 0.0000000 0.2634237 0.4580196 0.3908239 0.1750448
#> EL 0.3626537 0.2634237 0.0000000 0.3851451 0.2554068 0.9118653
#> ED 0.6302561 0.4580196 0.3851451 0.0000000 0.6974629 0.3897128
#> CL 0.3971935 0.3908239 0.2554068 0.6974629 0.0000000 0.3003636
#> CD 0.2805118 0.1750448 0.9118653 0.3897128 0.3003636 0.0000000
tidy_sym(m)
#> # A tibble: 36 × 3
#>    group1 group2 value
#>    <chr>  <chr>  <dbl>
#>  1 CD     EH     0.281
#>  2 CD     EP     0.175
#>  3 CD     EL     0.912
#>  4 CD     ED     0.390
#>  5 CD     CL     0.300
#>  6 CD     CD     1    
#>  7 CL     EH     0.397
#>  8 CL     EP     0.391
#>  9 CL     EL     0.255
#> 10 CL     ED     0.697
#> # … with 26 more rows
tidy_sym(make_lower_tri(m))
#> # A tibble: 15 × 3
#>    group1 group2 value
#>    <chr>  <chr>  <dbl>
#>  1 CD     EH     0.281
#>  2 CD     EP     0.175
#>  3 CD     EL     0.912
#>  4 CD     ED     0.390
#>  5 CD     CL     0.300
#>  6 CL     EH     0.397
#>  7 CL     EP     0.391
#>  8 CL     EL     0.255
#>  9 CL     ED     0.697
#> 10 ED     EH     0.630
#> 11 ED     EP     0.458
#> 12 ED     EL     0.385
#> 13 EL     EH     0.363
#> 14 EL     EP     0.263
#> 15 EP     EH     0.870

# }