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. You could also first use sampleRegular
The methods for Spatial* objects allow for easy creation of a data.frame with the coordinates and attributes; the default method only returns the attributes data.frame## S3 method for class 'Raster':
as.data.frame(x, row.names=NULL, optional=FALSE, xy=FALSE,
na.rm=FALSE, long=FALSE, ...)
## S3 method for class 'SpatialPolygons':
as.data.frame(x, row.names=NULL, optional=FALSE,
xy=FALSE, centroids=TRUE, sepNA=FALSE, ...)
## S3 method for class 'SpatialLines':
as.data.frame(x, row.names=NULL, optional=FALSE,
xy=FALSE, sepNA=FALSE, ...)
NULL
or a character vector giving the row names for the data frame. Missing values are not allowedTRUE
, setting row names and converting column names (to syntactic names: see make.names) is optionalTRUE
, also return the spatial coordinatesTRUE
, remove rows with NA values. This can be particularly useful for very large datasets with many NA valuesTRUE
, values are reshaped
from a wide to a long formatTRUE
return the centroids instead of all spatial coordinates (only relevant if xy=TRUE
)TRUE
the parts of the spatial objects are separated by lines that are NA
(only if xy=TRUE
and, for polygons, if centroids=FALSE
r <- raster(ncol=3, nrow=3)
r[] <- sqrt(1:ncell(r))
r[3:5] <- NA
as.data.frame(r)
s <- stack(r, r*2)
as.data.frame(s)
as.data.frame(s, na.rm=TRUE)
Run the code above in your browser using DataLab