I am trying to make a function that rescales a vector to lie between two argument lower and upper. And rescaling is done by linear transformation.
# rescales x to lie between lower and upper
rescale <- function(x, lower = 0, upper = 1){
slope <- (upper-lower)/(which.max(x)-which.min(x))
intercept <- slope*(x-which.max(x))+upper
y <- intercept + slope * x
return(list(new = y, coef = c(intercept = intercept, slope = slope)))
}
I have a feeling that I am not on the right track. Please give me some advice to make it right.