R/mean_norm.R

Defines functions mean_norm

Documented in mean_norm

#' @title Mean Normalization Function
#' @description mean_norm is a function to normalize the mean of the data.
#' After applying this function, the data is expected to be 0 mean.
#' @param a : for now it only supports vector and data.frame only
#' @author Suberlin Sinaga
#' @example
#' \dontrun{
#' a <- c(12,3,4,10,15)
#' mean_norm(a)
#' }
#' @export
mean_norm <- function(a, na.rm = FALSE) {
  if(na.rm){
    a <- a[!is.na(a)]
  }
  if (inherits(a, "numeric")) {
    res <- (a - mean(a))/(max(a) - min(a))
    res <- as_amartha(res)
  }else if(inherits(a, "data.frame")){
    len <- length(a)
    res <- a
    for (i in 1:length(a)){
      if (!is.numeric(a[[i]])) {
        res[,i] <- a[,i]
      } else{
        res[,i] <- (a[,i] - mean(a[,i]))/(max(a[,i])-min(a[,i]))
      }
    }
    res <- as_amartha(res)
  }else{
    stop('Provide me numeric vector or data.frame only! Factor or character type is not intended to be mean normalized...')
  }
  return(res)
}
blakcjack/amarthaPedia documentation built on Oct. 9, 2020, 7:56 a.m.