Standardize a variable by subtracting the mean and dividing by the standard deviation

standardize(x, na.rm = FALSE)

Arguments

x

A numeric vector

na.rm

A boolean equal to TRUE if ignore NA when calculating the mean and standard deviations. See mean and sd

Value

A vector of standardized numbers

Examples

x <- runif(10)
standardize(x)
#>  [1]  0.0338459 -1.2208602 -1.0098707  1.0133263  0.9246266  1.1173084
#>  [7]  1.0834942 -0.7880408 -1.2468939  0.0930641

x[1] <- NA
standardize(x, na.rm = TRUE)
#>  [1]         NA -1.1475736 -0.9486368  0.9589866  0.8753538  1.0570288
#>  [7]  1.0251462 -0.7394787 -1.1721201  0.0912937