rescale | R Documentation |
Rescale variables to a new range. Can also be used to reverse-score variables (change the keying/scoring direction), or to expand a range.
rescale(x, ...)
change_scale(x, ...)
## S3 method for class 'numeric'
rescale(
x,
to = c(0, 100),
multiply = NULL,
add = NULL,
range = NULL,
verbose = TRUE,
...
)
## S3 method for class 'data.frame'
rescale(
x,
select = NULL,
exclude = NULL,
to = c(0, 100),
multiply = NULL,
add = NULL,
range = NULL,
append = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = FALSE,
...
)
x |
A (grouped) data frame, numeric vector or factor. |
... |
Arguments passed to or from other methods. |
to |
Numeric vector of length 2 giving the new range that the variable will have after rescaling. To reverse-score a variable, the range should be given with the maximum value first. See examples. |
multiply |
If not |
add |
A vector of length 1 or 2. If not |
range |
Initial (old) range of values. If |
verbose |
Toggle warnings. |
select |
Variables that will be included when performing the required tasks. Can be either
If |
exclude |
See |
append |
Logical or string. If |
ignore_case |
Logical, if |
regex |
Logical, if |
A rescaled object.
select
argumentFor most functions that have a select
argument (including this function),
the complete input data frame is returned, even when select
only selects
a range of variables. That is, the function is only applied to those variables
that have a match in select
, while all other variables remain unchanged.
In other words: for this function, select
will not omit any non-included
variables, so that the returned data frame will include all variables
from the input data frame.
See makepredictcall.dw_transformer()
for use in model formulas.
Other transform utilities:
normalize()
,
ranktransform()
,
reverse()
,
standardize()
rescale(c(0, 1, 5, -5, -2))
rescale(c(0, 1, 5, -5, -2), to = c(-5, 5))
rescale(c(1, 2, 3, 4, 5), to = c(-2, 2))
# Specify the "theoretical" range of the input vector
rescale(c(1, 3, 4), to = c(0, 40), range = c(0, 4))
# Reverse-score a variable
rescale(c(1, 2, 3, 4, 5), to = c(5, 1))
rescale(c(1, 2, 3, 4, 5), to = c(2, -2))
# Data frames
head(rescale(iris, to = c(0, 1)))
head(rescale(iris, to = c(0, 1), select = "Sepal.Length"))
# One can specify a list of ranges
head(rescale(iris, to = list(
"Sepal.Length" = c(0, 1),
"Petal.Length" = c(-1, 0)
)))
# "expand" ranges by a factor or a given value
x <- 5:15
x
# both will expand the range by 10%
rescale(x, multiply = 1.1)
rescale(x, add = 0.5)
# expand range by different values
rescale(x, add = c(1, 3))
# Specify list of multipliers
d <- data.frame(x = 5:15, y = 5:15)
rescale(d, multiply = list(x = 1.1, y = 0.5))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.