Search code examples
rnormalize

normalize my table according to my ranges


I want to normalize all my table (not by column or row) according to the max and min of all table.

for ex:

enter image description here

in these whole table : max : 9 min: 0

so I want to normalize all these numbers between 0 and 9 to between 0 and 1!

Any helps?


Solution

  • The standard formula for such normalisation is

    (xi - min(x))/(max(x) - min(x))
    

    So for this data we can do

    vals <- unlist(df)
    max_val <- max(vals)
    min_val <- min(vals)
    df[] <- (vals - min_val)/(max_val - min_val)
    
    df
    #  column1 column2 column3
    #1  0.1111  0.3333  1.0000
    #2  0.2222  0.4444  0.6667
    #3  0.3333  0.6667  0.3333
    #4  0.4444  0.3333  0.1111
    #5  0.5556  0.0000  0.7778
    

    data

    df <- data.frame(column1 = 1:5, column2 = c(3, 4, 6, 3, 0), 
                     column3 = c(9, 6, 3, 1, 7))