rescale | R Documentation |
Rescale values to a new range
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),
...
)
x |
A numeric vector or |
to |
The lower and upper bounds of the new range. Default |
from |
(optional) The lower and upper bounds of the old range (calculated from |
... |
Additional arguments (not used). |
A new object whose values have been rescaled.
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
.
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.