nibbleNA: Replace NA values in a grid with the mode of the neighboring...

nibbleNAR Documentation

Replace NA values in a grid with the mode of the neighboring values

Description

This function replaces NA values with the mode of the 8 closest neighbors. NA cells that are surrounded by NA will be unaltered but the function can be reapplied to iteratively nibble values into a patch of NA, filling it in from the edges inward.

Usage

nibbleNA(x)
## S3 method for class 'grid'
nibbleNA(x)
## S3 method for class 'matrix'
nibbleNA(x)

Arguments

x

A grid or matrix with some NA values

Value

this function returns an object of the same class as x (either a grid or matrix) in which NA cells that abut cells with values will have the mode of the neighboring values. Ties are broken deterministically based on position in this order: right, left, bottom, top, top left, bottom right, bottom left, and top right.

Note

This function should only be applied to categorical grids where cells are likely to have identical values. Replacing NA in continuous grids would be better done with a kernel smooth. See gridio2::calckernel and gridio2::gaussiansmooth for a couple of possibilities.

This function currently always uses all 8 neighbors.

Author(s)

Ethan Plunkett

See Also

eliminatesmallpatches uses this function to eliminate patches below a certain size threshold.

Examples

  set.seed(1)
  g <- asgrid(matrix(runif(n = 48)> .7, 6, 8), xll = 0, yll = 0, cellsize = 10)
  g$m[2, 2] <- NA
  plot(g)
  plot(nibbleNA(g))

ethanplunkett/gridprocess documentation built on Feb. 1, 2024, 2:24 a.m.