values_to_raster {mbg} | R Documentation |
Insert values into a raster
Description
Insert a vector or matrix of values into an ID spatRaster
Usage
values_to_raster(x, id_raster)
Arguments
x |
Vector, matrix, data.frame, or data.table of values that will be inserted into
the ID raster. The length of x must be exactly divisible by
|
id_raster |
ID raster showing the outline of the study area, created using
|
Details
The length of the vector or matrix must be a multiple of the number of non-NA pixels in the ID raster. Values from the vector/matrix are then inserted into the non-NA pixels of the spatRaster.
Value
SpatRaster with the same outline as the ID raster and (# values / # non-NA pixels in the ID raster) layers.
See Also
Examples
# Example ID raster with 10 rows and 10 columns, and 99 valid pixels
example_id_raster <- terra::rast(matrix(c(seq_len(99), NA), nrow = 10))
# Inserting 99 values yields a spatRaster with 1 layer
mbg::values_to_raster(stats::rnorm(99), example_id_raster)
# Inserting 99 * 3 values yields a spatRaster with 3 layers
mbg::values_to_raster(seq_len(99 * 3), example_id_raster)
# Trying to insert values with length not divisible by 99 yields an error
try(mbg::values_to_raster(seq_len(100), example_id_raster))