raster (version 1.0.4)

calc: Calculate

Description

Calculate values for a new RasterLayer, from a single RasterLayer or from a RasterStack.

Usage

calc(x, fun, ...)

Arguments

x
a Raster* object
fun
a function
...
Additional arguments. See Details

Value

  • a RasterLayer object

Methods

1) x is a RasterLayer object; fun is a function calc(x, fun, filename='', ...) rll{ x a RasterLayer object fun The function to be applied filename Output filename (can be absent for RasterLayers that can be stored in memory) ... Additional arguments. See below } 2) x is a RasterStack object; fun is a function calc(x, fun, filename='',...) rll{ x a RasterStack object } Other items as above 3) x is a RasterStack object; fun is a list of functions calc(x, fun, filename='', ...) rll{ x a RasterStack object fun a list of the functions to be applied } Other items as above The following additional arguments can be passed, to replace default values for this function rll{ format Character. Output file type. See writeRaster datatype Character. Output data type. See dataType overwrite Logical. If TRUE, "filename" will be overwritten if it exists progress Character. Valid values are "text", "tcltk", "windows" (on that platform only) and "" }

Details

if x = "RasterLayer", fun should be a function that can take a single value as input, and returns a single value (e.g. sqrt). if x = "RasterStack", fun should be a function that takes a vector of values as input, and returns a single value (e.g. sum). See (overlay) to use functions that refer to specific layers, like (function(a,b,c){a + sqrt(b) / c})

See Also

overlay , reclass, Arith-methods, Math-methods

Examples

Run this code
r <- raster(ncols=36, nrows=18)
r[] <- 1:ncell(r)

# multiply values with 10
fun <- function(x) { x * 10 }
rc <- calc(r, fun)

# set values below 100 to NA. 
fun <- function(x) { x[x<100] <- NA; return(x) }
rc <- calc(r, fun)

# set NA values to -9999
fun <- function(x) { x[is.na(x)] <- -9999; return(x)} 
rc <- calc(rc, fun)

r2 <- r * 2
s <- stack(r, r2)
rs <- calc(s, sum)

Run the code above in your browser using DataCamp Workspace