rescale: Rescale values to a new range

View source: R/ranges.R

rescaleR Documentation

Rescale values to a new range

Description

Rescale values to a new range

Usage

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),
  ...
)

## S3 method for class 'SpatRaster'
rescale(
  x,
  to = c(0, 1),
  from = range(values(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

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))

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


achubaty/amc documentation built on June 10, 2025, 1:10 p.m.