4/13/2022»»Wednesday

R Slotnames

4/13/2022
R Slotnames Average ratng: 3,8/5 3105 reviews

Support for gridded data in R in recent year has been best implemented with the raster package by Robert Hijmans. The raster package allows you to:

Setnames In R

Setnames in r

Names is a generic accessor function, and names.

  • read and write almost any commonly used raster data format using rgdal
  • perform typical raster processing operations such as resampling, projecting, filtering, raster math, etc.
  • work with files on disk that are too big to read into memory in R
  • run operations quickly since the package relies on back-end C code
  • How to define S4 Class? S4 class is defined using the setClass function. In R terminology, member variables are called slots. While defining a class, we need to set the name and the slots (along with class of the slot) it is going to have.
  • Analysis and spatial data in R one should readBivand et al. (2008)’s book Applied Spatial Data Analysis with R. All spatial data stored in the UScensus2000 suite are of the form SpatialPolygonsDataFrame (e.g., california.tract is a SpatialPolygonsDataFrame ob-ject). SpatialPolygonsDataFrame objects are a so-called S4 class object in R and con.

The home page for the raster package has links to several well-written vignettes and documentation for the package.

The raster package uses three classes / types of objects to represent raster data - RasterLayer, RasterStack, and RasterBrick - these are all S4 new style classes in R, just like sp classes.

There are new developments in raster world in R worth noting - a new package called stars (spatiotemporal tidy arrays with R) has been developed through an R-Consortium funded project to handle rasters in a way that is both sf and pipe-based workflow friendly.

Additionally, Robert Hijmans has a new in development package called terra as a replacement package for raster.

To familiarize ourselves with the raster package, let’s create an empty RasterLayer object - in order to do this we have to:* define the matrix (rows and columns)* define the spatial bounding box* provide values to the cells

R Slotnames

Note that typically we would be reading raster data in from a file rather than creating a raster from scratch. Run through code steps below to familiarize yourself with contsructing a RasterLayer from scratch.

raster uses a S4 slot structure with a RasterLayer object. Simply typing the name of the RasterLayer gives a summary of the object at the console.

5.0.1 Exploring raster objects

  1. what is the minimal data required to define a RasterLayer?
  2. What is odd here about the CRS?
  3. How do we pull out just the CRS for our r rasterLayer?
  4. Building on this, what is the code to pull out just our xmin value in our extent, or bounding box?

5.0.2 Answer

  1. number columns, number rows, and extent - though the raster package will use defaults if values aren’t provided

  2. We didn’t provide one - raster uses default crs of WGS84 if you don’t provide a crs

Slotnames

5.0.3 Manipulating raster objects

So far we just have a container with no values (try plotting what we have so far) - let’s provide values to the cells using the runif function to derive random values from the uniform distribution

Notice the different output now when typing ‘r?’ An important point to make here is that objects in the raster package can be either in memory or on disk - not the value for our RasterLayer r of ‘data source’. If this were a large raster on disk, the value would be the path to the file on disk.

We can test this as well using methods in raster (and test if the raster has values too):

We can plot now as well with base R plot:

We can also overwrite the cell values for our raster:

You can access raster values via direct indexing or line, column indexing - take a minute to see how this works using raster r we just created - the syntax is:

How is raster data storage unlike a matrix in R? You can create a matrix with same dimensions and values and compare if you want:

Slotnames

5.0.4 RasterBricks and Rasterstacks

RasterBrick and RasterStack are the two additional classes in raster and handle multiple raster layers. They differ from each other in how they handle multiple layers, file formats they support, their representation and their processing speed. A RasterBrick typically is a single multi-layer file such as a multispectral image. A RasterStack can virtually connect several RasterLayer objects in memory and allows pixel-based calculations on separate raster layers, while a RasterBrick has to refer to a single multi-layer file or multi-layer object. Note that methods that operate on either a RasterStack or RasterBrick usually return a RasterBrick, and processing will be mor efficient on a RasterBrick object.

It’s easy to manipulate our RasterLayer to make a couple new layers, and then stack layers:

R Slotnames

Same process for generating a raster brick (here I make layers and create a RasterBrick in same step):