rescale: Rescale linearly to new minimum and maximum

Description Usage Arguments Details Value See Also Examples

View source: R/rescale.R

Description

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

Usage

1
2
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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 Nov. 9, 2017, 3:21 p.m.