0

Suppose I want to scale the columns in a dataframe. I can perhaps use builtin/internal functions to do same but i wanted to understand how i can do it using basic building blocks(using vectorized code). I want to return the input dataframe transformed such that each column in it has scaled values ranging from 0 and 1. The following was my unsuccessful attempt:

scaleColumns <- function(x){
    #if max(x) - min(x) = 0, then return the vector as it is. 
    ifelse(test = (max(x) - min(x))!=0, yes = (x- min(x))/(max(x) - min(x)), no = x)
}

X <- data.frame(c(6:9,3:6))
X_norm = apply(X, 2, scaleColumns)
X_norm 

Any ideas?

umbersar
  • 1,821
  • 3
  • 24
  • 34

1 Answers1

1

Your test is of length 1 and ifelse returns output of same length as test. Use if/else here. Also instead of apply column-wise use lapply since apply coerces dataframe to matrix.

scaleColumns <- function(x){
   if((max(x) - min(x))!=0) (x- min(x))/(max(x) - min(x)) else x
}

X <- data.frame(a = c(6:9,3:6))
X[] = lapply(X, scaleColumns)
X
#      a
#1 0.500
#2 0.667
#3 0.833
#4 1.000
#5 0.000
#6 0.167
#7 0.333
#8 0.500
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • even though i had made an error in writing the problem statement but this provided enough pointers towards the solution i was looking for – umbersar Jul 13 '20 at 11:08