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)
    [1] -1.0000000 -0.7777778 -0.5555556 -0.3333333 -0.1111111 0.1111111
    [7] 0.3333333 0.5555556 0.7777778 1.0000000 NA

    [1] -1.2222222 0.2222222**

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

  • $\begingroup$ @Glen_b How is this not off-topic as being about the correct R code? $\endgroup$
    – Nick Cox
    Feb 27, 2014 at 9:58
  • 1
    $\begingroup$ @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.) $\endgroup$
    – Glen_b
    Feb 27, 2014 at 10:16
  • 1
    $\begingroup$ @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. $\endgroup$
    – Nick Cox
    Feb 27, 2014 at 10:35

1 Answer 1


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

  • 1
    $\begingroup$ 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. $\endgroup$
    – Glen_b
    Feb 27, 2014 at 9:32
  • $\begingroup$ 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... $\endgroup$
    – BurninLeo
    Feb 27, 2014 at 9:40
  • $\begingroup$ The tag wasn't there when you answered, but the fact that it was coursework was clear from the question. $\endgroup$
    – Glen_b
    Feb 27, 2014 at 10:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.