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

Description Usage Arguments Value Author(s) Examples

View source: R/resca.R

Description

[Stable]

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.

Usage

1
2
3
4
5
6
7
8
9
resca(
  .data = NULL,
  ...,
  values = NULL,
  new_min = 0,
  new_max = 100,
  na.rm = TRUE,
  keep = TRUE
)

Arguments

.data

The dataset. Grouped data is allowed.

...

Comma-separated list of unquoted variable names that will be rescaled.

values

Optional vector of values to rescale

new_min

The minimum value of the new scale. Default is 0.

new_max

The maximum value of the new scale. Default is 100

na.rm

Remove NA values? Default to TRUE.

keep

Should all variables be kept after rescaling? If false, only rescaled variables will be kept.

Value

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

Author(s)

Tiago Olivoto tiagoolivoto@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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")) %>%
  means_by(ENV, GEN) %>%
  group_by(ENV) %>%
  resca(ends_with("L")) %>%
  head(n = 13)

metan documentation built on Nov. 10, 2021, 9:11 a.m.