Normalize the numeric variables in a data frame

normalize(data, new_min = 0, new_max = 1)

Arguments

data

a data frame.

new_min

minimum for the transformed variables.

new_max

maximum for the transformed variables.

Value

a data frame

Details

normalize transforms all the numeric variables in a data frame to have the same minimum and maximum values. By default, this will be a minimum of 0 and maximum of 1. Character variables and factors are left unchanged.

Note

Use this function to be transform variables into a given range. The default is [0, 1], but [-1, 1], [0, 100], or any other range is permissible.

Examples

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 <- normalize(cars74)
head(cars74_st)
#>                auto       mpg  cyl      disp        hp      drat        wt
#> 1         Mazda RX4 0.4510638 cyl6 0.2217511 0.2049470 0.5253456 0.2830478
#> 2     Mazda RX4 Wag 0.4510638 cyl6 0.2217511 0.2049470 0.5253456 0.3482485
#> 3        Datsun 710 0.5276596 cyl4 0.0920429 0.1448763 0.5023041 0.2063411
#> 4    Hornet 4 Drive 0.4680851 cyl6 0.4662010 0.2049470 0.1474654 0.4351828
#> 5 Hornet Sportabout 0.3531915 cyl8 0.7206286 0.4346290 0.1797235 0.4927129
#> 6           Valiant 0.3276596 cyl6 0.3838863 0.1872792 0.0000000 0.4978266
#>        qsec       vs        am   gear  carb
#> 1 0.2333333 V-shaped    manual gears4 carb4
#> 2 0.3000000 V-shaped    manual gears4 carb4
#> 3 0.4892857 straight    manual gears4 carb1
#> 4 0.5880952 straight automatic gears3 carb1
#> 5 0.3000000 V-shaped automatic gears3 carb2
#> 6 0.6809524 straight automatic gears3 carb1