nibbleNA | R Documentation |
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.
nibbleNA(x)
## S3 method for class 'grid'
nibbleNA(x)
## S3 method for class 'matrix'
nibbleNA(x)
x |
A grid or matrix with some NA values |
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.
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.
Ethan Plunkett
eliminatesmallpatches
uses this function to eliminate patches
below a certain size threshold.
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.