# rescale a vector x to lie between arguments LOWER and UPPER in R [duplicate]

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.0000000 -0.7777778 -0.5555556 -0.3333333 -0.1111111 0.1111111  0.3333333 0.5555556 0.7777778 1.0000000 NA$coef
 -1.2222222 0.2222222**


So does anyone has ideas on what the "slope" and "intercept" here should be?

• Please add the self-study tag and read its tag wiki info. What have you tried? – Glen_b -Reinstate Monica Feb 27 '14 at 9:30
• I've added the tag myself, based on the clear statement in the question that this is study, and its routine textbook-like nature. Please still read the tag-wiki info and modify your question accordingly. – Glen_b -Reinstate Monica Feb 27 '14 at 9:36
• @Glen_b How is this not off-topic as being about the correct R code? – Nick Cox Feb 27 '14 at 9:58
• @NickCox I'd have chosen to answer the question as a general question about how to do such rescaling (i.e. the on-topic aspect of the question). Mentioning code doesn't make the statistical aspects of a question disappear! It's really only in the absence of a statistical component that it's off topic. Now that it's been answered with code, you should feel free to close it. (Come to think of it, it's may well be a duplicate as well.) – Glen_b -Reinstate Monica Feb 27 '14 at 10:16
• @Glen_b Clearly even experienced people can draw the line between on- and off-topic in slightly different places. I didn't see there being a statistical issue here, as if there is uncertainty it's about a point in elementary mathematics. – Nick Cox Feb 27 '14 at 10:35

slope is a factor describing the target-range / source-range. If you need a range of 0..2, than you would write:

slope = (2 - 0) / (max(data) - min(data))


The intercept is the offset from zero. So if your target range is 1..3 (instead of 0..2) then your intercept would be 1 (all values +1).

Finally, you the formula only works if your data starts with 0. Elsewise, you'd have to subtract the original intercept.

rescale = function(x, lower=0, upper=1) {
xMin = min(x)
xMax = max(x)
factor = (upper - lower) / (xMax - xMin)
return((x-xMin) * factor + xMin)
}


Best, BurninLeo

• This is (as plainly stated in the question) coursework. The self-study tag wiki info suggests we should not supply code or complete solutions, but help guide the asker to the solution with hints, or perhaps pseudocode if necessary. Please read the guidelines at the link and provide hints, rather than just doing the OP's assignment. – Glen_b -Reinstate Monica Feb 27 '14 at 9:32
• Arg - I am sorry! Actually I missed the self-study tag. Gladly, my function is only practical and does not solve all the partial questions... – BurninLeo Feb 27 '14 at 9:40
• The tag wasn't there when you answered, but the fact that it was coursework was clear from the question. – Glen_b -Reinstate Monica Feb 27 '14 at 10:12