R/centre.R

Defines functions centre

Documented in centre

#' Function to centre or standardise a vector
#'
#' @param x Numeric vector
#' 
#' @param standardise Should \code{x} be rescaled to have a mean of 0 and a standard deviation of 1? Also known as the z-score
#'
#' @return Vector of same length as \code{x}
#' 
#' @author Shona Wilde
#' 
#' @export

centre <- function(x, standardise = F) {
  
  mean <- mean(x, na.rm = T)
  
  x <- x-mean
  
  if (standardise) {
    
    sd <- sd(x)
    
    x <- x/sd
    
  }
  
  return(x)
  
}
shonawilde/shonarrr documentation built on Jan. 29, 2025, 1:27 a.m.