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?