Using the matrix and array classes
A raster is essentially a matrix with spatial reference information. Similarly, a multiband raster is essentially a three-dimensional array with spatial reference information. Therefore, before proceeding with spatial rasters, we will cover some prerequisite material on working with these (simpler) objects in this section—matrices and arrays. Moreover, as we shall see later, matrices and arrays are common data structures with many uses in R.
Representing two-dimensional data with a matrix
A matrix
object is a two-dimensional collection of elements, all of the same type (as opposed to a data.frame
object; see the previous chapter), where the number of elements in all rows (and, naturally, all columns) is identical. Matrix objects have many uses in R. For example, certain functions take matrices as their arguments (such as the focal
function to filter rasters) or return matrices (such as the extract
function to extract raster values; we will meet both these...