# RAT and cell values in R's raster-objects

I really struggle to understand some basic concepts in the structure of how `R's` `raster`-objects save values. Namely the `RasterLayer`, the `RasterBrick` and the `RasterStack`.

I understand that the values (when manually creating a raster) can just be of type `numeric, integer, logical or factor`. When I create a raster with numeric values like this:

``````raster_numeric = raster(nrows = 6, ncols = 6, res = 0.5,
xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5,
vals = seq(0.1,3.6,0.1))
``````

I get this structure:

``````class      : RasterLayer
dimensions : 6, 6, 36  (nrow, ncol, ncell)
resolution : 0.5, 0.5  (x, y)
extent     : -1.5, 1.5, -1.5, 1.5  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
source     : memory
names      : layer
values     : 0.1, 3.6  (min, max)
``````

And apparently no `attributes`-slot.

Whereas, when I use factorial values (copied from the amazing Lovelace et al. book) I get, what to my understanding is a Raster-Attribute-Table (RAT). So why is this?

``````class      : RasterLayer
dimensions : 6, 6, 36  (nrow, ncol, ncell)
resolution : 0.5, 0.5  (x, y)
extent     : -1.5, 1.5, -1.5, 1.5  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
source     : memory
names      : layer
values     : 1, 3  (min, max)
attributes :
ID VALUE
1  clay
2  silt
3  sand
``````

What exactly is this RAT and can `raster`-objects really save just one value because in the end each cell can be references to a row in the RAT and thereby hold many values of different type?

A RAT is just a table that crosswalks the numeric values contained in an integer raster to descriptive attributes. It is not realistic to have a RAT for a true floating-point raster (eg., elevation) and really does not make much sense to. If one wants to represent a process as nominal than you just reclassify it into the desired ranges.

A good way to understand Raster Attribute Tables (RAT) is to work through creating one. The ID represent the unique values in a given raster and are fixed but, you can have multiple columns representing different attributes. In this example we create a raster with four values, ratify the raster and then define a few attributes that may emulate attribution between say, landcover a soil type that could result from a combination between two rasters representing these two nominal class types.

``````library(raster)
soil <- raster(extent(571823, 616763, 4423540, 4453690), resolution=100)
soil[] <- sample(c(1:4), ncell(soil), replace = TRUE)
soil
``````

Here we ratify our soil raster, pull the resulting data.frame, add some attributes to it and then add it back to the data.

``````( soil <- ratify(soil) )    # create a raster with a RAT
rat <- levels(soil)[]  # pull the data.frame from the raster RAT

# modify data.frame object
rat\$covertype <- c("riparian", "wetland", "wetland", "riparian")
rat\$soil <- c("clay", "silt", "sand", "sand")

# Write data.frame back to raster RAT and print results
levels(soil) <- rat
soil
``````

Honestly, other than for plotting, I find RAT rasters of very limited use. You can accomplish the same types of analysis by just leveraging a stand alone data.frame object (that is not buried in the raster) to query the values of the raster.

"each cell can be references to a row" - no, in a single Raster Layer each cell is a reference to exactly one row in the attribute table. So in your example each cell is either clay, silt, or sand (or possible `NA` missing data if not in the corresponding numeric range).

• exactly, but there could possibly be more than one column for each row. So a cell-Id references a row in the RAT, which itself can hold many different values (columns) ? – Robin Kohrs Jan 13 at 18:48