Learn R Programming

raster (version 1.6-10)

indexing: Get or replace values of Raster objects

Description

object[i] can be used to access values of a Raster* object, using cell numbers. You can also use row and column numbers as index, using object[i,j] or object[i,] or object[,j]. In addition you can supply a SpatialPolygons, SpatialLines or SpatialPoints object (as in link{extract}. If you supply a RasterLayer, its values will be used as logical (TRUE/FALSE) indices if both Raster objects have the same extent and resolution; otherwise the cell values within the extent of the RasterLayer are returned. You can also set values of a RasterLayer object, when i is a vector of cell numbers or a Spatial* object.

Arguments

Methods

Either get values x[i] x[i,j] Arguments rll{ x a Raster* object i cell number(s), row number(s), a (logical) RasterLayer, Spatial* object j column number(s) (only available if i is (are) a row number(s)) } Or set values x[i] <- value x[i,j] <- value Arguments rll{ x a RasterLayer object i cell number(s), row number(s), Extent, Spatial* object j columns number(s) (only available if i is (are) a row number(s)) value new cell value(s) }

See Also

getValues, setValues, extract

Examples

Run this code
r <- raster(ncol=10, nrow=5)
r[] <- 1:ncell(r) * 2
r[1,] <- 1
r[,1] <- 2
r[1,1] <- 3

as.matrix(r)

r[1]
r[1:10]
r[1,]
r[,1]
r[1:2, 1:2]

s <- stack(r,sqrt(r))
s[1:3]

Run the code above in your browser using DataLab