R/pull_triangle.R

Defines functions pull_lower_triangle pull_upper_triangle pull_triangle

Documented in pull_lower_triangle pull_triangle pull_upper_triangle

#' @include utilities.R replace_triangle.R
NULL
#' Pull Lower and Upper Triangular Part of a Matrix
#' @description Returns the lower or the upper triangular part of a
#'   (correlation) matrix.
#' @param x a (correlation) matrix
#' @param diagonal logical. Default is FALSE. If TRUE, the matrix diagonal is
#'   included.
#' @param triangle the triangle to pull. Allowed values are one of
#'   "upper" and "lower".
#' @return an object of class \code{cor_mat_tri}, which is a data frame
#' @seealso \code{\link{replace_triangle}()}
#' @examples
#'
#' # Data preparation
#' #::::::::::::::::::::::::::::::::::::::::::
#' mydata <- mtcars %>%
#'   select(mpg, disp, hp, drat, wt, qsec)
#' head(mydata, 3)
#'
#' # Compute correlation matrix and pull triangles
#' #::::::::::::::::::::::::::::::::::::::::::
#' # Correlation matrix
#' cor.mat <- cor_mat(mydata)
#' cor.mat
#'
#' # Pull lower triangular part
#' cor.mat %>% pull_lower_triangle()
#'
#' # Pull upper triangular part
#' cor.mat %>% pull_upper_triangle()
#'
#'
#' @describeIn pull_triangle returns either the lower or upper triangular part of a matrix.
#' @export
pull_triangle <- function(x, triangle = c("lower", "upper"),
                          diagonal = FALSE){

  triangle.to.pull <- match.arg(triangle)
  triangle.to.replace <- ifelse(
    triangle.to.pull == "lower", "upper", "lower"
  )
  triangle.class <- paste0(triangle.to.pull, "_tri")

  res <- x %>%
    replace_triangle(triangle.to.replace, by = "", diagonal = diagonal) %>%
    add_class(triangle.class)
  res
}

#' @describeIn pull_triangle returns an object of class \code{upper_tri}, which
#'   is a data frame containing the upper triangular part of a matrix.
#' @export
pull_upper_triangle <- function(x,  diagonal = FALSE){
  x %>% pull_triangle("upper", diagonal = diagonal)
}

#' @describeIn pull_triangle returns an object of class \code{lower_tri}, which
#'   is a data frame containing the lower triangular part of a matrix.
#' @export
pull_lower_triangle <- function(x, diagonal = FALSE){
  x %>% pull_triangle("lower", diagonal = diagonal)
}

Try the rstatix package in your browser

Any scripts or data that you put into this service are public.

rstatix documentation built on Feb. 16, 2023, 6:10 p.m.