general_rescale: Rescale a Numeric Vector

Description Usage Arguments Value Examples

View source: R/general_rescale.R

Description

Rescale a numeric vector with the option to make signed (-1, 1, or 0) and retain zero as neutral.

Usage

1
2
3
4
5
6
7
8
9
general_rescale(
  x,
  lower = -1,
  upper = 1,
  mute = NULL,
  keep.zero = lower < 0,
  sign = FALSE,
  ...
)

Arguments

x

A numeric vector.

lower

An upper limit to rescale to.

upper

A lower limit to rescale to.

mute

A positive value greater than 1 to lower the extremes and pull the fractions up. This becomes the denominator in a power to raise each element by (sign is retained) where the numerator is 1. This is useful for mellowing out the extremes.

keep.zero

logical. If TRUE the zeros are kept as neutral.

sign

logical. If TRUE the vector will be scaled as (-1, 1, or 0)

...

ignored.

Value

Returns a rescaled vector of the same length as x.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
general_rescale(c(1, 0, -1))
general_rescale(c(1, 0, -1, 1.4, -2))
general_rescale(c(1, 0, -1, 1.4, -2), lower = 0, upper = 1)
general_rescale(c(NA, -4:3))
general_rescale(c(NA, -4:3), keep.zero = FALSE)
general_rescale(c(NA, -4:3), keep.zero = FALSE, lower = 0, upper = 100)

## mute extreme values
set.seed(10)
x <- sort(c(NA, -100, -10, 0, rnorm(10, 0, .1), 10, 100), na.last = FALSE)
general_rescale(x)
general_rescale(x, mute = 5)
general_rescale(x, mute = 10)
general_rescale(x, mute = 100)

sentimentr documentation built on Oct. 12, 2021, 9:06 a.m.