rescale: Rescale a numeric vector

Description Usage Arguments Details Value Examples

View source: R/rescale.R

Description

rescale returns a rescaled numeric vector.

Usage

1
2
rescale(x, method = c("normal", "minmax"), mean = 0, sd = 1, min = 0,
  max = 1)

Arguments

x

A numeric vector.

method

A character string indicating the desired method of rescaling with "normal" being the default. This must be (an abbreviation of) one of the strings "normal" or "minmax". See Details for more information.

mean

The desired mean value for normal-style scaling. Used only when method="normal".

sd

The desired standard deviation value for normal-style scaling. Used only when method="normal".

min

The desired minimum value for min/max-style scaling. Used only when method="minmax".

max

The desired maximum value for min/max-style scaling. Used only when method="minmax".

Details

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:

The arguments mean, sd, min, and max are used based on method. rescale offers a couple of different options for method:

Value

The output of rescale is a rescaled numeric vector with the same length as x.

Examples

 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)

danielmarcelino/SciencesPo documentation built on Oct. 20, 2019, 1:15 a.m.