Standardize the numeric variables in a data frame
standardize(data, mean = 0, sd = 1, include_dummy = FALSE)
a data frame.
mean of the transformed variables.
standard deviation of the transformed variables.
logical. If TRUE
, transform
dummy coded (0,1) variables.
a data frame
standardize
transforms all the numeric variables
in a data frame to have the same mean and standard deviation.
By default, this will be a mean of 0 and standard deviation of 1.
Character variables and factors are left unchanged. By default,
dummy coded variables are also left unchanged. Use
include_dummy=TRUE
to transform these variables as well.
head(cars74)
#> auto mpg cyl disp hp drat wt qsec vs am
#> 1 Mazda RX4 21.0 cyl6 160 110 3.90 2.620 16.46 V-shaped manual
#> 2 Mazda RX4 Wag 21.0 cyl6 160 110 3.90 2.875 17.02 V-shaped manual
#> 3 Datsun 710 22.8 cyl4 108 93 3.85 2.320 18.61 straight manual
#> 4 Hornet 4 Drive 21.4 cyl6 258 110 3.08 3.215 19.44 straight automatic
#> 5 Hornet Sportabout 18.7 cyl8 360 175 3.15 3.440 17.02 V-shaped automatic
#> 6 Valiant 18.1 cyl6 225 105 2.76 3.460 20.22 straight automatic
#> gear carb
#> 1 gears4 carb4
#> 2 gears4 carb4
#> 3 gears4 carb1
#> 4 gears3 carb1
#> 5 gears3 carb2
#> 6 gears3 carb1
cars74_st <- standardize(cars74)
head(cars74_st)
#> auto mpg cyl disp hp drat
#> 1 Mazda RX4 0.1508848 cyl6 -0.57061982 -0.5350928 0.5675137
#> 2 Mazda RX4 Wag 0.1508848 cyl6 -0.57061982 -0.5350928 0.5675137
#> 3 Datsun 710 0.4495434 cyl4 -0.99018209 -0.7830405 0.4739996
#> 4 Hornet 4 Drive 0.2172534 cyl6 0.22009369 -0.5350928 -0.9661175
#> 5 Hornet Sportabout -0.2307345 cyl8 1.04308123 0.4129422 -0.8351978
#> 6 Valiant -0.3302874 cyl6 -0.04616698 -0.6080186 -1.5646078
#> wt qsec vs am gear carb
#> 1 -0.610399567 -0.7771651 V-shaped manual gears4 carb4
#> 2 -0.349785269 -0.4637808 V-shaped manual gears4 carb4
#> 3 -0.917004624 0.4260068 straight manual gears4 carb1
#> 4 -0.002299538 0.8904872 straight automatic gears3 carb1
#> 5 0.227654255 -0.4637808 V-shaped automatic gears3 carb2
#> 6 0.248094592 1.3269868 straight automatic gears3 carb1