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(mtcars)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

mtcars_st <- normalize(mtcars)
head(mtcars_st)
#>                         mpg cyl      disp        hp      drat        wt
#> Mazda RX4         0.4510638 0.5 0.2217511 0.2049470 0.5253456 0.2830478
#> Mazda RX4 Wag     0.4510638 0.5 0.2217511 0.2049470 0.5253456 0.3482485
#> Datsun 710        0.5276596 0.0 0.0920429 0.1448763 0.5023041 0.2063411
#> Hornet 4 Drive    0.4680851 0.5 0.4662010 0.2049470 0.1474654 0.4351828
#> Hornet Sportabout 0.3531915 1.0 0.7206286 0.4346290 0.1797235 0.4927129
#> Valiant           0.3276596 0.5 0.3838863 0.1872792 0.0000000 0.4978266
#>                        qsec vs am gear      carb
#> Mazda RX4         0.2333333  0  1  0.5 0.4285714
#> Mazda RX4 Wag     0.3000000  0  1  0.5 0.4285714
#> Datsun 710        0.4892857  1  1  0.5 0.0000000
#> Hornet 4 Drive    0.5880952  1  0  0.0 0.0000000
#> Hornet Sportabout 0.3000000  0  0  0.0 0.1428571
#> Valiant           0.6809524  1  0  0.0 0.0000000