These are shorthand methods that call other methods that should normally be used, such as getValues, extract, crop.

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 an Extent, SpatialPolygons, SpatialLines or SpatialPoints object.

If drop=TRUE (the default) cell values are returned (a vector for a RasterLayer, a matrix for a RasterStack or RasterBrick). If drop=FALSE a Raster* object is returned that has the extent covering the requested cells, and with all other non-requested cells within this extent set to NA.

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.

Double brackes '[[ ]]' can be used to extract one or more layers from a multi-layer object.

Methods

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))
drop If TRUE, cell values are returned. Otherwise, a Raster* object is returned

Examples

r <- raster(ncol=10, nrow=5)
values(r) <- 1:ncell(r) 

r[1]
#> [1] 1
r[1:10]
#>  [1]  1  2  3  4  5  6  7  8  9 10
r[1,]
#>  [1]  1  2  3  4  5  6  7  8  9 10
r[,1]
#> [1]  1 11 21 31 41
r[1:2, 1:2]
#> [1]  1  2 11 12

s <- stack(r, sqrt(r))
s[1:3]
#>      layer.1  layer.2
#> [1,]       1 1.000000
#> [2,]       2 1.414214
#> [3,]       3 1.732051
s[[2]]
#> class      : RasterLayer 
#> dimensions : 5, 10, 50  (nrow, ncol, ncell)
#> resolution : 36, 36  (x, y)
#> extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> crs        : +proj=longlat +datum=WGS84 +no_defs 
#> source     : memory
#> names      : layer.2 
#> values     : 1, 7.071068  (min, max)
#>