getValueByCell {enmSdmX} | R Documentation |
Get or assign values to cells in a raster
Description
These functions get values from a raster at specific cells, or values to specific cells.
Usage
getValueByCell(x, cell, format = "raster")
setValueByCell(x, val, cell, format = "raster")
Arguments
x |
A |
cell |
Cell indices. There must be one per value in |
format |
The type of cell indexing used. This can be either "raster" for row indexing (default) or "matrix" for column indexing. Row indexing (the default for rasters), starts with cell "1" in the upper left, cell "2" is to its right, and so on. Numbering then wraps around to the next row. Column indexing (the default for matrices) has the cell "1" in the upper left corner of the matrix. The cell "2" is below it, and so on. The numbering then wraps around to the top of the next column. |
val |
One or more values. If more the number of cells specified is greater than the number of values in |
Value
A data frame (getValueByCell
) with cell numbers (in row format), or a SpatRaster
(setValueByCell
).
See Also
Examples
library(terra)
x <- rast(nrow=10, ncol=10)
x[] <- round(10 * runif(100))
cell <- c(1, 20, 40, 80)
getValueByCell(x, cell = cell)
getValueByCell(x, cell = cell, format = 'matrix')
y <- setValueByCell(x, val = 20, cell = cell)
plot(y)
z <- setValueByCell(x, val = 30, cell = cell, format = 'matrix')
plot(c(x, y, z))