R/row_scale.R

Defines functions row_scale

Documented in row_scale

#' Title
#'
#' @param x
#' @param center
#' @param scale
#' @param add_attr
#' @param rows
#' @param cols
#' @param drop.na.rows
#'
#' @return
#' @export
#'
#' @examples
row_scale = function(x,
                     center = TRUE,
                     scale = TRUE,
                     add_attr = TRUE,
                     rows = NULL,
                     cols = NULL,
                     drop.na.rows = T) {

  if (!is.null(rows) && !is.null(cols)) {
    x <- x[rows, cols, drop = FALSE]
  } else if (!is.null(rows)) {
    x <- x[rows, , drop = FALSE]
  } else if (!is.null(cols)) {
    x <- x[, cols, drop = FALSE]
  }

  rm = Matrix::rowMeans(x, na.rm = TRUE)
  if (scale) {
    csd = apply(x, 1, sd)
    #csd = matrixStats::rowSds(x, center = rm)
  } else {
    csd = rep(1, length = length(rm))
  }
  if (!center) {
    rm = rep(0, length = length(rm))
  }
  x = (x - rm) / csd

  if (drop.na.rows) {
    x <- x[which(Matrix::rowSums(is.na(x))<ncol(x)),,drop=F]
  }

  if (add_attr) {
    if (center) {
      attr(x, "scaled:center") <- rm
    }
    if (scale) {
      attr(x, "scaled:scale") <- csd
    }
  }
  return(x)
}
Close-your-eyes/scexpr documentation built on April 21, 2023, 10:27 a.m.