raster (version 1.1.7)

focal: Focal

Description

Calculate values for the neighborhood of focal cells. focalNA keeps original values except where these are NA. New values are computed with function fun, using the values of the cells in the neigborhood of a focal cell (including the focal cell).

Usage

focal(x, ngb=3, fun=mean, na.rm=TRUE, filename="", ...) 
focalNA(x, ngb=3, fun=mean, recursive=FALSE, maxrec=0, filename="", ...)

Arguments

x
A RasterLayer object
ngb
Neighborhood size. A neighborhood is expressed in number of cells in a single direction or in two direction from the focal cell. I.e. ngb=3 refers to 2 cells at each side of the focal cell, qeen's case, 9 cells in total. This is equivalent t
fun
The function to be applied
na.rm
Logical. If TRUE, NA will be removed from focal computations. The result will only be NA if all focal cells are NA
recursive
Logical. If TRUE, the function will continue recursively untill there are no more cells with NA
maxrec
Integer. Maximum number of recursions (if recursive=TRUE)
filename
Output filename for a new raster
...
additional arguments. See Details.

Value

  • A new RasterLayer object (in the R environment), and in some cases the side effect of a new file on disk.

Details

The following additional arguments can be passed, to replace default values for this function rll{ overwrite Logical. If TRUE, "filename" will be overwritten if it exists format Character. Output file type. See writeRaster datatype Character. Output data type. See dataType progress Character. Valid values are "text", "tcltk", "windows" (on that platform only) and "" }

See Also

focalFilter, focalValues

Examples

Run this code
r <- raster(ncols=36, nrows=18)
r[] <- runif(ncell(r)) 
rf <- focal(r, fun=mean, ngb=3)

Run the code above in your browser using DataCamp Workspace