R: Rescale a Numeric Vector

general_rescale {sentimentr} | R Documentation

## Rescale a Numeric Vector

### Description

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

### Usage

    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

    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)

[Package sentimentr version 2.4.2 Index]