Skip to contents

This function uses splines to fill NA cells in a raster based on the values of nearby cells. Depending on the method used, not all NA cells can be filled.

Usage

# S4 method for class 'GRaster'
fillNAs(
  x,
  lambda = NULL,
  method = "bilinear",
  min = -Inf,
  max = Inf,
  cells = Inf
)

Arguments

x

A GRaster.

lambda

Either NULL (default), or a numeric value > 0: If NULL, then the function will use leave-one-out crossvalidation to find the optimal value.

method

Character: Type of spline, either "bilinear" (default), "bicubic", or "RST" (regularized splines with tension). Partial matching is used and case is ignored.

Note: The RST method will often display warnings, but thesecan be ignored.

min, max

Numeric: Lowest and highest values allowed in the interpolated values. Values outside these bounds will be truncated to the minimum/maximum value(s) allowed. The default imposes no constraints. For multi-layered rasters, you can supply a single value for min and/or max, or multiple values (one per layer). Values will be recycled if there are fewer than one or them per layer in the raster.

cells

Integer or numeric integer: Number of cells away from the non-NA cells to fill. For example, if cells = 2, then only cells within a 2-cell buffer of non-NA cells will be filled. The default is Inf (fill all possible cells–some methods may not be able to do this, depending on the configuration of the raster).

Value

A GRaster.

See also

Examples

if (grassStarted()) {

# Setup
library(terra)

# Elevation raster:
madElev <- fastData("madElev")

# Convert a SpatRaster to a GRaster:
elev <- fast(madElev)

### Fill NAs:
biline <- interp <- fillNAs(elev)
bicube <- interp <- fillNAs(elev, method = "bicubic")
rst <- interp <- fillNAs(elev, method = "rst")

maps <- c(elev, biline, bicube, rst)
names(maps) <- c("original", "bilinear", "bicubic", "RST")
plot(maps)

### Constrain interpolated values to > 0
constrained <- fillNAs(elev, min = 0)

# Compare unconstrained and constrained:
minmax(biline)
minmax(constrained)

### Interpolate to only first 10 cells away from non-NA cells:
restrained <- fillNAs(elev, cells = 10)

maps <- c(elev, restrained)
names(maps) <- c("Original", "within 10 cells")
plot(maps)

}