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

as.matrix

Get a vector, matrix, or array with raster cell values


Description

as.vector returns a vector of cell values. For a RasterLayer it is equivalent to getValues(x).

as.matrix returns all values of a Raster* object as a matrix. For RasterLayers, rows and columns in the matrix represent rows and columns in the RasterLayer object. For other Raster* objects, the matrix returned by as.matrix has columns for each layer and rows for each cell.

as.array returns an array of matrices that are like those returned by as.matrix for a RasterLayer

If there is insufficient memory to load all values, you can use getValues or getValuesBlock to read chunks of the file.

as.matrix and as.vector can also be used to obtain the coordinates from an Extent object.

Usage

as.matrix(x, ...)
as.array(x, ...)

## S4 method for signature 'Extent'
as.vector(x, mode='any')

## S4 method for signature 'Raster'
as.vector(x, mode='any')

Arguments

x

Raster* or (for as.matrix and as.vector) Extent object

mode

Character string giving an atomic mode (such as "numeric" or "character") or "list", or "any". Note: this argument is currently ignored!

...

additional arguments:

maxpixels Integer. To regularly subsample very large objects

transpose Logical. Transpose the data? (for as.array only)

Value

matrix, array, or vector

Examples

r <- raster(ncol=3, nrow=3)
values(r) <- 1:ncell(r)
as.matrix(r)
s <- stack(r,r)
as.array(s)
as.vector(extent(s))

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.