Search code examples
rrangemaxminscaling

Unable to get a range scale function working


I'm trying to apply the range scaling formula on across columns of a dataset. The formula I'm using is the Mean Normalisation of the available methods for range scaling.

formula

I've currently tried using the following code:

rangescale <- function(r){
  colmean <- apply(r,2,mean)
  colsd <- apply(r, 2, sd)
  cv <- sweep(r, 2, colmean, "-")
  xmax <- apply(r, 2, max)
  xmin <- apply(r,2, min)
  ma.mi <- apply(xmax, 2, xmin, "-")
  rv <- sweep(cv, 2, ma.mi, "/")
  return(rv)
}

It is giving me:

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'xmin' of mode 'function' was not found 

It doesn't look like that base r has the minimum function, are there other methods to get the min and max of columns? Or are there problems I've missed out entirely?


Solution

  • You could create a function to apply scaling

    rangescale <- function(x) (x  - mean(x))/(max(x) - min(x))
    

    and apply it column-wise

    apply(r, 2, rangescale)
    

    Or with dplyr

    library(dplyr)
    r %>% summarise_all(rangescale)
    

    To pass entire dataset to the function we can take apply inside the function

    rangescale <- function(r) {
      apply(r, 2, function(x) (x  - mean(x))/(max(x) - min(x)))
    }
    

    and then call it as :

    r1 <- rangescale(r)