Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

extractIndex

Indexing to extract values of a Raster* object


Description

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

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

See Also

Examples

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

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

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

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.