1

I have a dataframe with 7 different features. I need to scale the values but for for each isolated feature. I am currently using the "rescale". However, I can only fit it to one column and transform all the data with these min and max values for 1 feature!!

How can I use the MinMaxScaler for each column/feature in R?

1 Answers1

1

You can use your minmax function in a loop over all columns, for instance.

  • Base R
minmax <- function(x, na.rm = TRUE) {
    return((x- min(x)) /(max(x)-min(x)))
}

df <- mtcars
dfs1 <- as.data.frame(sapply(df, minmax))
  • Or with dplyr::mutate_all()
require(dplyr)
dfs2 <- df %>% 
  dplyr::mutate_all(.funs = "minmax")

> all.equal(dfs1$mpg, dfs2$mpg)
[1] TRUE
Jlopes
  • 116
  • 5