Commit ce6def46 authored by houyun's avatar houyun
Browse files

helper function to simplify md_tbl

parent 157f37e4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -139,6 +139,7 @@ export(secondary_y_axis)
export(set_corrplot_style)
export(set_default_style)
export(set_secondary_axis)
export(simplify)
export(suffix_with)
export(theme_hy)
export(trim_diag)
+33 −0
Original line number Diff line number Diff line
@@ -268,3 +268,36 @@ trim_duplicate <- function(md, diag = TRUE) {

  md[!duplicated(id), ]
}

#' @param keep logical, the output matrix remains symmetric if
#' the input matrix is symmetric.
#' @rdname Helper_function
#' @export
simplify <- function(md, keep = TRUE) {
  stopifnot(is_md_tbl(md))
  if (empty(md)) {
    stop("Empty data input.", call. = FALSE)
  }

  rnm <- attr(md, "row_names")
  cnm <- attr(md, "col_names")
  if (!identical(rnm, cnm)) {
    keep <- FALSE
  }

  if (isTRUE(keep)) {
    row_and_cols <- unique(c(md$.rownames, md$.conames))
    attr(md, "row_names") <- rnm[rnm %in% row_and_cols]
    attr(md, "col_names") <- cnm[cnm %in% row_and_cols]
  } else {
    rows <- unique(md$.rownames)
    cols <- unique(md$.colnames)
    attr(md, "row_names") <- rows
    attr(md, "col_names") <- cols
    if (!identical(rows, cols)) {
      attr(md, "type") <- "full"
      attr(md, "diag") <- TRUE
    }
  }
  md
}
+6 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
\alias{trim_diag}
\alias{filter_func}
\alias{trim_duplicate}
\alias{simplify}
\title{Helper function}
\usage{
row_names(md)
@@ -47,6 +48,8 @@ trim_diag(md)
filter_func(..., type = "full", diag = FALSE)

trim_duplicate(md, diag = TRUE)

simplify(md, keep = TRUE)
}
\arguments{
\item{md}{a matrix_data object.}
@@ -58,6 +61,9 @@ trim_duplicate(md, diag = TRUE)
\item{...}{other parameters.}

\item{type}{character, "full" (default), "upper", "lower" or "diag".}

\item{keep}{logical, the output matrix remains symmetric if
the input matrix is symmetric.}
}
\description{
Helper function for matrix_data object