rescale | R Documentation |
Linerarly rescale values of a numeric vector so that the result have new prescribed minimum and maximum.
rescale(
x,
nmin = 0,
nmax = 1,
na.rm = TRUE,
from = range(x, na.rm = na.rm),
to = c(nmin, nmax),
coef = FALSE
)
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
|
from |
numeric vector of length two, range from which to rescale.
Defaults to the range of |
to |
numeric vector of length two, output range |
coef |
logical, should a vector with coefficients of the transformation be returned instead |
Imports rescale
from package scales.
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.
scale
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.