Skip to contents

Centre then divide by the standard error of the values in a numeric variable

Usage

standardize_vars(data, vars, names = NULL)

Arguments

data

a dataframe with the columns to standardize

vars

a character vector of the numeric variables in data to standardize

names

names for the created variables

Value

the original dataframe containing new columns of the standardized variables with tidyverse code attached

See also

Author

Zhaoming Su

Examples

standardized <- standardize_vars(iris, var = c("Sepal.Width", "Petal.Width"))
cat(code(standardized))
#> iris |> dplyr::mutate(Sepal.Width.std = scale(Sepal.Width)[, 1], .after = Sepal.Width) |>   dplyr::mutate(Petal.Width.std = scale(Petal.Width)[, 1], .after = Petal.Width)
head(standardized)
#>   Sepal.Length Sepal.Width Sepal.Width.std Petal.Length Petal.Width
#> 1          5.1         3.5      1.01560199          1.4         0.2
#> 2          4.9         3.0     -0.13153881          1.4         0.2
#> 3          4.7         3.2      0.32731751          1.3         0.2
#> 4          4.6         3.1      0.09788935          1.5         0.2
#> 5          5.0         3.6      1.24503015          1.4         0.2
#> 6          5.4         3.9      1.93331463          1.7         0.4
#>   Petal.Width.std Species
#> 1       -1.311052  setosa
#> 2       -1.311052  setosa
#> 3       -1.311052  setosa
#> 4       -1.311052  setosa
#> 5       -1.311052  setosa
#> 6       -1.048667  setosa