R/standardize_data.r

Defines functions standardize_data.data.frame standardize_data

Documented in standardize_data standardize_data.data.frame

# standardize_data generic function ----
standardize_data <- function(data, center = TRUE, scale = TRUE, ...) UseMethod("standardize_data", data)

# standardize_data data.frame method ----
standardize_data.data.frame <- function(data, center = TRUE, scale = TRUE, ...) {
	
  # run checks ----
  if (!is.logical(center)) {
    center <- TRUE
    warning("center must be a logical value! Default for center is TRUE.")
  }
  if (!is.logical(scale)) {
	scale <- TRUE
	warning("scale must be a logical value! Default for scale is TRUE.")
  }
	
  # run standardization ----
  numeric_vars <- unlist(lapply(data, is.numeric))
  scaled_data <- scale(data[,numeric_vars], center = center, scale = scale)
  out <- cbind.data.frame(scaled_data, data[,!numeric_vars])

  # return output ----
  if (requireNamespace("tibble", quietly = TRUE)) {
	out <- tibble::as_tibble(out)
  }

  return(out)	
}
ha-pu/supportR documentation built on Sept. 13, 2020, 5:52 p.m.