I am trying to rescales x to lie between lower and upper
rescale <- function(x, lower = 0, upper = 1){
slope <- ??
intercept <- ??
y <- intercept + slope * x
return(list(new = y, coef = c(intercept = intercept, slope = slope)))
}
And this is the hint from my professor: The linear transformation should map the minimum of x to lower and the maximum of x to upper. (Hint: what is the equation for a line passing through two points?) Note in calculating the minimum and maximum that x may contain NA values; make sure your function can handle this. And the return would look like:
**> rescale(c(1:10, NA), -1, 1)
$new
[1] -1.0000000 -0.7777778 -0.5555556 -0.3333333 -0.1111111 0.1111111
[7] 0.3333333 0.5555556 0.7777778 1.0000000 NA
$coef
[1] -1.2222222 0.2222222**
So does anyone has ideas on what the "slope" and "intercept" here should be?