Search code examples
rtransformscale

Scale row values ignoring a column


I have the following data.frame:

char   A     B     C
a      1     2     3
b     300  239   444
c      15   25    32

I need to normalize the data by transforming the row values into intervals between 0 and 1 ignoring the first column but keeping that in the results.

How could I do that?


Solution

  • You can do this:

    bind_cols(
      df %>% select(char),
      as.data.frame(t(apply(df[,-1],1,\(x) (x-min(x))/(max(x)-min(x))))))
    )
    

    Output:

      char        A         B C
    1    a 0.000000 0.5000000 1
    2    b 0.297561 0.0000000 1
    3    c 0.000000 0.5882353 1