Skip to contents

fasterRaster GRasters can represent double-floating point numeric values, integers, or categorical data.

Double-floating point values

Double-floating point values are accurate to about the 15th to 17th decimal place. These are called "double" rasters in fasterRaster and DCELL rasters in GRASS. These rasters typically take the most memory. All "numeric" values in R are double-floating point values.

Floating point values

Less common that double-floating point rasters, floating point rasters are accurate to about the 7th decimal place. These are called "float" rasters in fasterRaster and FCELL rasters in GRASS. These rasters typically take less memory than double-floating point rasters.

Integers

Rasters that represent integers are called "integer" rasters in fasterRaster and CELL rasters in GRASS. You can force a raster to be an integer using as.int(). Some of the functions in app() function will also return integer-type rasters. Integer rasters typically take the least memory.

Categories

Categorical rasters (also called "factor" rasters) are actually integer rasters, but have an associated attribute table that maps each integer value to a category label, such as "wetland" or "forest". The table has at least two columns. The first is integer values, and (by default) the second is category names. This second column is the "active" category column that is used for plotting and in some functions. The active column can be changed using activeCat<-.

Functions relevant to raster data types

  • activeCat(), activeCats(), and activeCat<- can be used to see or assign which column in a "levels" table associated with a categorical raster is used as category labels.

  • addCats() adds information to the "levels" table using data.table::merge() (same as merge()).

  • addCats<- add new levels to a "levels" table.

  • as.int(), as.float(), and as.doub() coerce a raster to an integer, float, or double.

  • catNames() reports the column names of the "levels" table of each layer of a raster.

  • cats() returns the entire "levels" table of a categorical raster.

  • combineCats() combines levels of two or more categorical or integer rasters.

  • combineLevels(): Combine the "levels" tables of two or more categorical GRasters.

  • complete.cases() finds rows in the levels table that have no NAs.

  • datatype() returns the data type of a GRaster.

  • droplevels() removes "unused" levels in a "levels" table.

  • freq(): Frequency of each category across cells of a raster

  • is.factor() indicates if the raster is a categorical raster.

  • is.int(), is.float(), and is.doub() indicate if values in a a raster are integers, floating-point, or double-floating point precision.

  • levels() returns the "levels" table of a categorical raster (just the value column and the active column).

  • levels<- and categories() can be used to assign categories to an integer raster and make it categorical (i.e., a "factor" raster).

  • match(), %in%, and %notin%: Find which cells of a GRaster match or do not match certain category labels

  • missing.cases() finds rows in the levels table that have at least one NA.

  • missingCats() finds values in categorical rasters that do not have a category assigned to them.

  • nlevels() returns the number of levels represented by a categorical raster.

  • subst(): Re-assign category levels

Saving rasters to disk

You can potentially save substantial space on disk by setting the datatype argument in writeRaster() to an appropriate value when saving a raster. This argument allows for finer "divisions" than just integer/float/double-float, so depending on the range of values in your raster, you can optimize file size by selecting the one that best matches the values in the raster. See the documentation for writeRaster() for more information.

Examples

if (grassStarted()) {

# Setup
library(sf)
library(terra)

# Example data
madElev <- fastData("madElev")
madForest2000 <- fastData("madForest2000")
madCoast0 <- fastData("madCoast0")
madRivers <- fastData("madRivers")
madDypsis <- fastData("madDypsis")

### GRaster properties
######################

# convert SpatRasters to GRasters
elev <- fast(madElev)
forest <- fast(madForest2000)

# plot
plot(elev)

dim(elev) # rows, columns, depths, layers
nrow(elev) # rows
ncol(elev) # columns
ndepth(elev) # depths
nlyr(elev) # layers

res(elev) # resolution

ncell(elev) # cells
ncell3d(elev) # cells (3D rasters only)

topology(elev) # number of dimensions
is.2d(elev) # is it 2D?
is.3d(elev) # is it 3D?

minmax(elev) # min/max values

# name of object in GRASS
sources(elev)

# "names" of the object
names(elev)

# coordinate reference system
crs(elev)

# extent (bounding box)
ext(elev)

# data type
datatype(elev)

# assigning
copy <- elev
copy[] <- pi # assign all cells to the value of pi
copy

# concatenating multiple GRasters
rasts <- c(elev, forest)
rasts

# adding a raster "in place"
add(rasts) <- ln(elev)
rasts

# subsetting
rasts[[1]]
rasts[["madForest2000"]]

# assigning
rasts[[4]] <- elev > 500

# number of layers
nlyr(rasts)

# names
names(rasts)
names(rasts) <- c("elev_meters", "forest", "ln_elev", "high_elevation")
rasts

### GVector properties
######################

# convert sf vectors to GVectors
coast <- fast(madCoast4)
rivers <- fast(madRivers)
dypsis <- fast(madDypsis)

# extent
ext(rivers)

W(rivers) # western extent
E(rivers) # eastern extent
S(rivers) # southern extent
N(rivers) # northern extent
top(rivers) # top extent (NA for 2D rasters like this one)
bottom(rivers) # bottom extent (NA for 2D rasters like this one)

# coordinate reference system
crs(rivers)
st_crs(rivers)

# column names and data types
names(coast)
datatype(coast)

# name of object in GRASS
sources(rivers)

# points, lines, or polygons?
geomtype(dypsis)
geomtype(rivers)
geomtype(coast)

is.points(dypsis)
is.points(coast)

is.lines(rivers)
is.lines(dypsis)

is.polygons(coast)
is.polygons(dypsis)

# dimensions
nrow(rivers) # how many spatial features
ncol(rivers) # hay many columns in the data frame

# number of geometries and sub-geometries
ngeom(coast)
nsubgeom(coast)

# 2- or 3D
topology(rivers) # dimensionality
is.2d(elev) # is it 2D?
is.3d(elev) # is it 3D?

# Update values from GRASS
# (Reads values from GRASS... will not appear to do anything in this case)
coast <- update(coast)

### operations on GVectors
##########################

# convert to data frame
as.data.frame(rivers)
as.data.table(rivers)

# subsetting
rivers[c(1:2, 5)] # select 3 rows/geometries
rivers[-5:-11] # remove rows/geometries 5 through 11
rivers[ , 1] # column 1
rivers[ , "NAM"] # select column
rivers[["NAM"]] # select column
rivers[1, 2:3] # row/geometry 1 and column 2 and 3
rivers[c(TRUE, FALSE)] # select every other geometry (T/F vector is recycled)
rivers[ , c(TRUE, FALSE)] # select every other column (T/F vector is recycled)

# removing data table
noTable <- dropTable(rivers)
noTable
nrow(rivers)
nrow(noTable)

# Refresh values from GRASS
# (Reads values from GRASS... will not appear to do anything in this case
# since the rivers object is up-to-date):
rivers <- update(rivers)

# Concatenating multiple vectors
rivers2 <- rbind(rivers, rivers)
dim(rivers)
dim(rivers2)

}