1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
\name{factors}
\docType{methods}
\alias{is.factor}
\alias{is.factor,Raster-method}
\alias{is.factor,RasterStack-method}
\alias{as.factor}
\alias{as.factor,RasterLayer-method}
\alias{levels}
\alias{levels,Raster-method}
\alias{levels,RasterStack-method}
\alias{levels<-}
\alias{levels<-,Raster-method}
\alias{asFactor}
\alias{asFactor,RasterLayer-method}
\alias{factorValues}
\alias{ratify}
\alias{ratify,Raster-method}
\alias{deratify}
\title{Factors}
\description{
These functions allow for defining a RasterLayer as a categorical variable. Such a RasterLayer is linked to other values via a "Raster Attribute Table" (RAT). Thus the cell values are an index, whereas the actual values of interest are in the RAT. The RAT is a data.frame. The first column in the RAT ("ID") has the unique cell values of the layer; this column should normally not be changed. The other columns can be of any basic type (factor, character, integer, numeric or logical). The functions documented here are mainly available such that files with a RAT can be read and processed; currently there is not too much further support. Whether a layer is defined as a factor or not is currently ignored by almost all functions. An exception is the 'extract' function (when used with option df=TRUE).
Function 'levels' returns the RAT for inspection. It can be modified and set using \code{levels <- value} (but use caution as it is easy to mess things up).
\code{as.factor} and \code{ratify} create a layer with a RAT table. Function 'deratify' creates a single layer for a (or each) variable in the RAT table.
}
\usage{
is.factor(x)
as.factor(x)
levels(x)
\S4method{ratify}{Raster}(x, filename="", count=FALSE, ...)
factorValues(x, v, layer=1, att=NULL, append.names=FALSE)
deratify(x, att=NULL, layer=1, complete=FALSE, drop=TRUE, fun='mean', filename='', ...)
asFactor(x, ...)
}
\arguments{
\item{x}{Raster* object}
\item{v}{integer cell values}
\item{layer}{integer > 0 indicating which layer to use (in a RasterStack or RasterBrick)}
\item{att}{numeric or character. Which variable(s) in the RAT table should be used. If \code{NULL}, all variables are extracted. If using a numeric, skip the first two default columns}
\item{append.names}{logical. Should names of data.frame returned by a combination of the name of the layer and the RAT variables? (can be useful for multilayer objects}
\item{filename}{character. Optional}
\item{count}{logical. If \code{TRUE}, a columns with frequencies is added}
\item{...}{additional arguments as for \code{\link{writeRaster}}}
\item{complete}{logical. If \code{TRUE}, the layer returned is no longer a factor}
\item{drop}{logical. If \code{TRUE} a factor is converted to a numerical value if possible}
\item{fun}{character. Used to get a single value for each class for a weighted RAT table. 'mean', 'min', 'max', 'smallest', or 'largest'}
}
\value{
Raster* object; list (levels); boolean (is.factor); matrix (factorValues)
}
\note{asFactor is deprecated and should not be used}
\examples{
set.seed(0)
r <- raster(nrow=10, ncol=10)
values(r) <- runif(ncell(r)) * 10
is.factor(r)
r <- round(r)
f <- as.factor(r)
is.factor(f)
x <- levels(f)[[1]]
x
x$code <- letters[10:20]
levels(f) <- x
levels(f)
f
r <- raster(nrow=10, ncol=10)
values(r) = 1
r[51:100] = 2
r[3:6, 1:5] = 3
r <- ratify(r)
rat <- levels(r)[[1]]
rat$landcover <- c("Pine", "Oak", "Meadow")
rat$code <- c(12,25,30)
levels(r) <- rat
r
# extract values for some cells
i <- extract(r, c(1,2, 25,100))
i
# get the attribute values for these cells
factorValues(r, i)
# write to file:
# rr <- writeRaster(r, rasterTmpFile(), overwrite=TRUE)
# rr
# create a single-layer factor
x <- deratify(r, "landcover")
x
is.factor(x)
levels(x)
}
\keyword{methods}
\keyword{spatial}
|