Description Usage Arguments Details Value Examples
rescale
returns a rescaled numeric vector.
1 2 |
x |
A numeric vector. |
method |
A character string indicating the desired method of rescaling
with |
mean |
The desired mean value for normal-style scaling.
Used only when |
sd |
The desired standard deviation value for normal-style scaling.
Used only when |
min |
The desired minimum value for min/max-style scaling.
Used only when |
max |
The desired maximum value for min/max-style scaling.
Used only when |
rescale
returns a rescaled version of the numeric vector x
.
NA
values in x
are ignored during the rescaling process but
are preserved in the output.
rescale
is designed to be readable from the function call.
For example:
rescale(x, "normal", mean=0, sd=1)
can be read as
"Rescale x using a normal-style
transformation with mean 0 and standard deviation 1".
rescale(x, "minmax", min=0, max=1)
can be read as
"Rescale x using a min/max-style
transformation with min 0 and max 1".
The arguments mean
, sd
, min
, and max
are used
based on method
. rescale
offers a couple of different options
for method
:
normal: The default option that rescales x
using
a normal-style transformation into a distribution with mean mean
and standard deviation sd
. The default values for this rescaling
are mean=0
and sd=1
.
The explicit formula for this transformation is:
(x - μ_x)/(σ_x) * σ + μ
where μ_x is the sample mean of x
, σ_x
is the sample standard deviation of x
, μ is the desired
mean, and σ is the desired standard deviation.
minmax: rescales x
using a min/max-style
transformation into a distribution with minimum min
and maximum
max
. The default values for this rescaling are min=0
and max=1
.
The explicit formula for this transformation is:
((x - min_x)/(max_x - min_x)) * (max - min) + min
where min_x is the sample minimum of x
, max_x
is the sample maximum of x
, min is the desired minimum,
and max is the desired maximum.
The output of rescale
is a rescaled numeric vector with
the same length as x
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | set.seed(1337)
x <- rnorm(1e4)
summary(x)
#####
# Common use cases
#
# I want to rescale to a standard normal distribution!
x_normal <- rescale(x)
summary(x_normal)
mean(x_normal); sd(x_normal)
# I want to rescale to be between 0 and 1!
x_minmax <- rescale(x, "minmax")
summary(x_minmax)
min(x_minmax); max(x_minmax)
# I want to rescale to be between 300 and 850! (Weird but some credit scores do it!)
x_credit <- rescale(x, "minmax", min=300, max=850)
summary(x_credit)
min(x_credit); max(x_credit)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.