1\name{as.data.frame} 2 3\alias{as.data.frame} 4\alias{as.data.frame,Raster-method} 5\alias{as.data.frame,SpatialPolygons-method} 6\alias{as.data.frame,SpatialLines-method} 7 8 9\title{Get a data.frame with raster cell values, or coerce SpatialPolygons, Lines, or Points to a data.frame} 10 11\description{ 12\code{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 \code{as.matrix} has columns for each layer and rows for each cell. 13 14\code{as.array} returns an array of matrices that are like those returned by \code{as.matrix} for a RasterLayer 15 16If there is insufficient memory to load all values, you can use \code{\link{getValues}} or \code{\link{getValuesBlock}} to read chunks of the file. You could also first use \code{\link{sampleRegular}} 17 18The 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 19} 20 21\usage{ 22\S4method{as.data.frame}{Raster}(x, row.names=NULL, optional=FALSE, xy=FALSE, 23 na.rm=FALSE, long=FALSE, ...) 24 25\S4method{as.data.frame}{SpatialPolygons}(x, row.names=NULL, optional=FALSE, 26 xy=FALSE, centroids=TRUE, sepNA=FALSE, ...) 27 28\S4method{as.data.frame}{SpatialLines}(x, row.names=NULL, optional=FALSE, 29 xy=FALSE, sepNA=FALSE, ...) 30} 31 32\arguments{ 33 \item{x}{Raster* object} 34 \item{row.names}{\code{NULL} or a character vector giving the row names for the data frame. Missing values are not allowed} 35 \item{optional}{logical. If \code{TRUE}, setting row names and converting column names (to syntactic names: see make.names) is optional} 36 \item{xy}{logical. If \code{TRUE}, also return the spatial coordinates} 37 \item{na.rm}{logical. If \code{TRUE}, remove rows with NA values. This can be particularly useful for very large datasets with many NA values} 38 \item{long}{logical. If \code{TRUE}, values are \code{\link{reshape}d} from a wide to a long format} 39 \item{centroids}{logical. If \code{TRUE} return the centroids instead of all spatial coordinates (only relevant if \code{xy=TRUE})} 40 \item{sepNA}{logical. If \code{TRUE} the parts of the spatial objects are separated by lines that are \code{NA} (only if \code{xy=TRUE} and, for polygons, if \code{centroids=FALSE}} 41 \item{...}{Additional arguments (none)} 42} 43 44\value{ 45data.frame 46} 47 48 49 50\examples{ 51r <- raster(ncol=3, nrow=3) 52values(r) <- sqrt(1:ncell(r)) 53r[3:5] <- NA 54as.data.frame(r) 55s <- stack(r, r*2) 56as.data.frame(s) 57as.data.frame(s, na.rm=TRUE) 58} 59 60\keyword{spatial} 61\keyword{methods} 62