R/mean_center.R

Defines functions mean_center

Documented in mean_center

#' Mean center
#'
#' Mean-center a variable, i.e., subtract the mean of a numeric vector
#' from each value in the numeric vector
#'
#' @param x a numeric vector; though not thoroughly tested, the function
#' can accept a matrix as an input.
#' @examples
#' mean_center(1:5)
#' mean_center(1:6)
#' # if the input is a matrix
#' matrix(1:9, nrow = 3)
#' mean_center(matrix(1:9, nrow = 3))
#' @export
mean_center <- function(x) {
  output <- scale(x = x, center = TRUE, scale = FALSE)
  # if the output is a vector, return the vector
  if (dim(output)[2] == 1) {
    output <- as.vector(output)
  }
  return(output)
}

Try the kim package in your browser

Any scripts or data that you put into this service are public.

kim documentation built on Oct. 9, 2023, 5:08 p.m.