R/utils.R

Defines functions mapper scaling_zscore

Documented in scaling_zscore

#' @title Normalize values in a vector by the z-score method
#'
#' @description \code{scaling_zscore} Normalize values in a vector by the z-score method.
#'
#' @usage
#' scaling_zscore(x)
#'
#' @param x A numeric vector.
#'
#' @return A numeric vector.
#'
#' @examples
#' scaling_zscore(rnorm(5))
#'
#' @export
#'
scaling_zscore = function(x){
  if (typeof(x) == "double"){

    if(length(x) == 1){ ## in case the vector has length 1 -- return 0
      return(0)
    }
    if(sd(x, na.rm = TRUE) > 0){
      return((x - mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE))
    } else{
      return((x - mean(x, na.rm = TRUE)))
    }
  } else {return(x)}
}
#' @title Normalize values in a vector by the modified z-score method.
#'
#' @description \code{scaling_modified_zscore} Normalize values by the modified z-score method (uses median and median absolute deviation instead meean)
#'
#' @usage
#' scaling_modified_zscore(x)
#'
#' @param x A numeric vector.
#'
#' @return A numeric vector.
#'
#' @examples
#' scaling_modified_zscore(rnorm(5))
#'
#' @export
#'
scaling_modified_zscore = function (x) {
  if (typeof(x) == "double") {
    if (mad(x, na.rm = TRUE) != 0) {
      return(0.6745 * (x - median(x))/mad(x))
    }
    else {
      return(0.6745 * (x - median(x)))
    }
  }
  else {
    return(x)
  }
}

mapper = function(df, value_col, name_col) setNames(df[[value_col]], df[[name_col]])
saeyslab/nichenetr documentation built on March 26, 2024, 9:22 a.m.