rescale: Rescale values to a new range

Description Usage Arguments Value Note Examples

View source: R/ranges.R

Description

Rescale values to a new range

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
rescale(x, to, from, ...)

## S3 method for class 'numeric'
rescale(x, to = c(0, 1), from = range(x, na.rm = TRUE, finite = TRUE), ...)

## S3 method for class 'RasterLayer'
rescale(
  x,
  to = c(0, 1),
  from = range(getValues(x), na.rm = TRUE, finite = TRUE),
  ...
)

Arguments

x

A numeric vector or Raster* object.

to

The lower and upper bounds of the new range. Default c(0,1).

from

(optional) The lower and upper bounds of the old range (calculated from x).

...

Additional arguments (not used).

Value

A new object whose values have been rescaled.

Note

Objects with values that are all equal (e.g., all zeroes) will be returned as-is. This behaviour differs from scales:rescale which would return a value of 0.5.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
rescale(50, from = c(0, 100), to = c(0, 1)) ## 0.5

x <- 0:100
rescale(x) # defaults to new range [0,1]
rescale(x, c(-1, 1))

f <- system.file("external/test.grd", package = "raster")
r <- raster::raster(f)
rescale(r) # defaults to new range [0,1]
rescale(r, c(-1, 1))

achubaty/amc documentation built on April 6, 2021, 6:56 a.m.