resca: Rescale a variable to have specified minimum and maximum...

View source: R/resca.R

rescaR Documentation

Rescale a variable to have specified minimum and maximum values



Helper function that rescales a continuous variable to have specified minimum and maximum values.

The function rescale a continuous variable as follows:

Rv_i = (Nmax - Nmin)/(Omax - Omin) * (O_i - Omax) + Nmax

Where Rv_i is the rescaled value of the ith position of the variable/ vector; Nmax and Nmin are the new maximum and minimum values; Omax and Omin are the maximum and minimum values of the original data, and O_i is the ith value of the original data.

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.


  .data = NULL,
  values = NULL,
  new_min = 0,
  new_max = 100,
  na.rm = TRUE,
  keep = TRUE



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.


A numeric vector if values is used as input data or a tibble if a data frame is used as input in .data.


Tiago Olivoto


# Rescale a numeric vector
resca(values = c(1:5))

 # Using a data frame
 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)

metan documentation built on March 7, 2023, 5:34 p.m.