raster (version 3.6-20)

as.matrix: Get a vector, matrix, or array with raster cell values


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.


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

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

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


matrix, array, or vector



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


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)


Run this code
r <- raster(ncol=3, nrow=3)
values(r) <- 1:ncell(r)
s <- stack(r,r)

Run the code above in your browser using DataCamp Workspace