Returns the lower or the upper triangular part of a (correlation) matrix.
pull_triangle(x, triangle = c("lower", "upper"), diagonal = FALSE)
pull_upper_triangle(x, diagonal = FALSE)
pull_lower_triangle(x, diagonal = FALSE)
a (correlation) matrix
the triangle to pull. Allowed values are one of "upper" and "lower".
logical. Default is FALSE. If TRUE, the matrix diagonal is included.
an object of class cor_mat_tri
, which is a data frame
pull_triangle()
: returns either the lower or upper triangular part of a matrix.
pull_upper_triangle()
: returns an object of class upper_tri
, which
is a data frame containing the upper triangular part of a matrix.
pull_lower_triangle()
: returns an object of class lower_tri
, which
is a data frame containing the lower triangular part of a matrix.
# Data preparation
#::::::::::::::::::::::::::::::::::::::::::
mydata <- mtcars %>%
select(mpg, disp, hp, drat, wt, qsec)
head(mydata, 3)
#> mpg disp hp drat wt qsec
#> Mazda RX4 21.0 160 110 3.90 2.620 16.46
#> Mazda RX4 Wag 21.0 160 110 3.90 2.875 17.02
#> Datsun 710 22.8 108 93 3.85 2.320 18.61
# Compute correlation matrix and pull triangles
#::::::::::::::::::::::::::::::::::::::::::
# Correlation matrix
cor.mat <- cor_mat(mydata)
cor.mat
#> # A tibble: 6 × 7
#> rowname mpg disp hp drat wt qsec
#> * <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 mpg 1 -0.85 -0.78 0.68 -0.87 0.42
#> 2 disp -0.85 1 0.79 -0.71 0.89 -0.43
#> 3 hp -0.78 0.79 1 -0.45 0.66 -0.71
#> 4 drat 0.68 -0.71 -0.45 1 -0.71 0.091
#> 5 wt -0.87 0.89 0.66 -0.71 1 -0.17
#> 6 qsec 0.42 -0.43 -0.71 0.091 -0.17 1
# Pull lower triangular part
cor.mat %>% pull_lower_triangle()
#> rowname mpg disp hp drat wt qsec
#> 1 mpg
#> 2 disp -0.85
#> 3 hp -0.78 0.79
#> 4 drat 0.68 -0.71 -0.45
#> 5 wt -0.87 0.89 0.66 -0.71
#> 6 qsec 0.42 -0.43 -0.71 0.091 -0.17
# Pull upper triangular part
cor.mat %>% pull_upper_triangle()
#> rowname mpg disp hp drat wt qsec
#> 1 mpg -0.85 -0.78 0.68 -0.87 0.42
#> 2 disp 0.79 -0.71 0.89 -0.43
#> 3 hp -0.45 0.66 -0.71
#> 4 drat -0.71 0.091
#> 5 wt -0.17
#> 6 qsec