R/normalise.R

Defines functions normalise

Documented in normalise

#' Function to normalise a numeric vector's range between 0 and 1. 
#' 
#' @param x Numeric vector
#' 
#' @param na.rm Remove NAs? Default is \code{TRUE}.
#' 
#' @return Numeric vector with the length of \code{x}.  
#' 
#' @seealso \href{http://stats.stackexchange.com/questions/70801/how-to-normalize-data-to-0-1-range}{stackexchange}
#' 
#' @author Stuart K. Grange
#' 
#' @export
normalise <- function(x, na.rm = TRUE) {
  
  if (length(x) == 1L) {
    # For when only one number is passed
    x <- 1
  } else {
    # Calculate max and min once
    min <- min(x, na.rm = na.rm)
    max <- max(x, na.rm = na.rm)
    
    # Apply normalising function
    x <- (x - min) / (max - min)
  }
  
  return(x)
  
}
skgrange/threadr documentation built on May 11, 2024, 12:16 p.m.