R/standardization.R

Defines functions zscore

Documented in zscore

# standardization =======================================

#' Safe centering and scaling: it avoids dividing by 0
#' @param x matrix to center and scale
#' @param center either numeric vector or logical
#' @param scale either numeric vector or logical. 
#' If numeric, the 0 entries are internally replaced by 1 
#' to avoid dividing by zero.
#' @return centred and scaled matrix
#' @export
zscore <- function(x, center = TRUE, scale = TRUE) {
    
    x = as.matrix(x)
    
    if ( isTRUE(scale) ) {
        scale = matrixStats::colSds(x)
    }
    
    if ( !is.logical(scale) ) {
        scale[scale == 0] = 1
    }
    
    base::scale(x, center = center, scale = scale)
}
unoe/noe documentation built on Nov. 5, 2019, 11:05 a.m.