rescale: Rescale linearly to new minimum and maximum

rescaleR Documentation

Rescale linearly to new minimum and maximum

Description

Linerarly rescale values of a numeric vector so that the result have new prescribed minimum and maximum.

Usage

rescale(
  x,
  nmin = 0,
  nmax = 1,
  na.rm = TRUE,
  from = range(x, na.rm = na.rm),
  to = c(nmin, nmax),
  coef = FALSE
)

Arguments

x

numeric vector to rescale

nmin

numeric scalar, new minimum, defaults to 0

nmax

numeric scalar, new maximum, defaults to 1

na.rm

logical, should the missing values be removed, defaults to TRUE

from

numeric vector of length two, range from which to rescale. Defaults to the range of x.

to

numeric vector of length two, output range

coef

logical, should a vector with coefficients of the transformation be returned instead

Details

Imports rescale from package scales.

Value

If coef is FALSE, which is the default, a vector of transformed values of x is returned. If it is TRUE then the list with components a and b is returned containing the coefficients of the transformation.

See Also

scale

Examples

set.seed(123)
x <- rnorm(20)
y <- rescale(x)
summary(x)
summary(y)
# coefficients of the transformation
k <- rescale(x, coef=TRUE)
k

# show how it works on the plot
plot(x, y, xlab="x", ylab="Result", main="Mechanics of 'rescale()'",
  axes=TRUE, asp=1, sub=paste("a=", k[1], ", b=", k[2], sep=""))
abline(v=c(min(x), max(x)), lty=2, col="gray")
abline(h=c(min(y), max(y)), lty=2, col="gray")
abline(a=k[1], b=k[2], col="gray")
text( (max(x) + min(x))/2, (max(y) + min(y))/2,
  label="a + bx", srt=tan(k[2]) / pi * 180, pos=3)

mbojan/mbtools documentation built on Oct. 16, 2023, 8:18 p.m.