# Rajarshi Guha # 2/2/05 # rescale a vector so that it lies between # the specified minimum and maximum rescale <- function(x, newrange) { if (!is.numeric(x) || !is.numeric(newrange)){ stop("Must supply numerics for the x and the new scale") } if (length(newrange) != 2) { stop("newrange must be a numeric vector with 2 elements") } newmin <- min(newrange) newmax <- max(newrange) oldrange <- range(x) if (oldrange[1] == oldrange[2]) { stop("The supplied vector is a constant. Cannot rescale") } y <- (newmax - newmin) / (oldrange[2] - oldrange[1]) newmin + (x - oldrange[1]) * y }