Helper function that rescales a continuous variable to have specified minimum and maximum values.
The function rescale a continuous variable as follows:
There are basically two options to use resca
to rescale a variable.
The first is passing a data frame to .data
argument and selecting one
or more variables to be scaled using ...
. The function will return the
original variables in .data
plus the rescaled variable(s) with the
prefix _res
. By using the function group_by
from dplyr
package it is possible to rescale the variable(s) within each level of the
grouping factor. The second option is pass a numeric vector in the argument
values
. The output, of course, will be a numeric vector of rescaled
values.
resca(
.data = NULL,
...,
values = NULL,
new_min = 0,
new_max = 100,
na.rm = TRUE,
keep = TRUE
)
A numeric vector if values
is used as input data or a tibble
if a data frame is used as input in .data
.
The dataset. Grouped data is allowed.
Comma-separated list of unquoted variable names that will be rescaled.
Optional vector of values to rescale
The minimum value of the new scale. Default is 0.
The maximum value of the new scale. Default is 100
Remove NA
values? Default to TRUE
.
Should all variables be kept after rescaling? If false, only rescaled variables will be kept.
Tiago Olivoto tiagoolivoto@gmail.com
# \donttest{
library(metan)
library(dplyr)
# Rescale a numeric vector
resca(values = c(1:5))
# Using a data frame
head(
resca(data_ge, GY, HM, new_min = 0, new_max = 1)
)
# Rescale within factors;
# Select variables that stats with 'N' and ends with 'L';
# Compute the mean of these variables by ENV and GEN;
# Rescale the variables that ends with 'L' whithin ENV;
data_ge2 %>%
select(ENV, GEN, starts_with("N"), ends_with("L")) %>%
mean_by(ENV, GEN) %>%
group_by(ENV) %>%
resca(ends_with("L")) %>%
head(n = 13)
# }
Run the code above in your browser using DataLab