R/standsdmu.R

Defines functions standsdmu

Documented in standsdmu

#' Standardization of data
#'
#' This function standardizes the data matrix so that each variable will have mean 0 and std 1
#'
#' @importFrom stats sd
#' @param dat Data matrix with rows are samples, columns are variables.
#' @return A data matrix with each variable having mean 0 and std 1.
#' @export
#' @examples
#' library(TestPMD)
#' data("covid")
#' mtb <- covid$metabolite
#' standsdmu(mtb)

standsdmu <- function(dat){
  mudat <- matrix(rep(apply(dat, 2, mean), dim(dat)[1]), nrow = dim(dat)[1], byrow=T)
  dat <- as.matrix((dat - mudat))
  for (icol in 1:dim(dat)[2]) {
    dat[,icol] <- dat[,icol]/stats::sd(dat[,icol])
  }
  return(dat)
}
YunhuiQi/TestPMD documentation built on May 5, 2022, 8:23 p.m.