spsanderson / healthyR.ai

healthyR.ai - AI package for the healthyverse
http://www.spsanderson.com/healthyR.ai/
Other
16 stars 6 forks source link

Scale Data #206

Closed spsanderson closed 2 years ago

spsanderson commented 2 years ago

Can use traditional 0 1 scale which hai_scale_zero_one_vec() does or use z-score standardization

X_new = (X - mean(x)) / sd(x)

spsanderson commented 2 years ago

Function:

hai_scale_zscore_vec <- function(.x){

  # Tidyeval ----
  x_term <- .x

  # Checks ----
  if (!is.numeric(x_term)){
    rlang::abort(
      message = "'.x' must be a numeric vector",
      use_cli_format = TRUE
    )
  }

  mu <- mean(x_term, na.rm = TRUE)
  s  <- sd(x_term, na.rm = TRUE)
  zscore_scaled <- ((x_term - mu) / s)

  # Return ----
  return(zscore_scaled)

}

Examples:

> hai_scale_zscore_vec(mtcars$mpg)
 [1]  0.15088482  0.15088482  0.44954345  0.21725341 -0.23073453 -0.33028740 -0.96078893
 [8]  0.71501778  0.44954345 -0.14777380 -0.38006384 -0.61235388 -0.46302456 -0.81145962
[15] -1.60788262 -1.60788262 -0.89442035  2.04238943  1.71054652  2.29127162  0.23384555
[22] -0.76168319 -0.81145962 -1.12671039 -0.14777380  1.19619000  0.98049211  1.71054652
[29] -0.71190675 -0.06481307 -0.84464392  0.21725341

image