Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

replace_triangle

Replace Lower and Upper Triangular Part of a Matrix


Description

Replace the lower or the upper triangular part of a (correlation) matrix.

Usage

replace_triangle(x, triangle = c("lower", "upper"), by = "", diagonal = FALSE)

replace_upper_triangle(x, by = "", diagonal = FALSE)

replace_lower_triangle(x, by = "", diagonal = FALSE)

Arguments

x

a (correlation) matrix

triangle

the triangle to replace. Allowed values are one of "upper" and "lower".

by

a replacement argument. Appropriate values are either "" or NA. Used to replace the upper, lower or the diagonal part of the matrix.

diagonal

logical. Default is FALSE. If TRUE, the matrix diagonal is included.

Value

an object of class cor_mat_tri, which is a data frame

Functions

  • replace_triangle: replaces the specified triangle by empty or NA.

  • replace_upper_triangle: replaces the upper triangular part of a matrix. Returns an object of class lower_tri.

  • replace_lower_triangle: replaces the lower triangular part of a matrix. Returns an object of class lower_tri

See Also

Examples

# Compute correlation matrix and pull triangles
#::::::::::::::::::::::::::::::::::::::::::
# Correlation matrix
 cor.mat <- mtcars %>%
   select(mpg, disp, hp, drat, wt, qsec) %>%
   cor_mat()
 cor.mat

 # Replace upper triangle by NA
#::::::::::::::::::::::::::::::::::::::::::
cor.mat %>% replace_upper_triangle(by = NA)


# Replace upper triangle by NA and reshape the
# correlation matrix to have unique combinations of variables
#::::::::::::::::::::::::::::::::::::::::::
cor.mat %>%
  replace_upper_triangle(by = NA) %>%
  cor_gather()

rstatix

Pipe-Friendly Framework for Basic Statistical Tests

v0.7.0
GPL-2
Authors
Alboukadel Kassambara [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.