
Last chance! 50% off unlimited learning
Sale ends in
calc(x, fun, ...)
x
is a RasterLayer object; fun
is a function
calc(x, fun, filename='', ...)
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='',...)
x
a RasterStack object
}
Other items as above
3) x
is a RasterStack object; fun
is a list of functions
calc(x, fun, filename='', ...)
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
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. "text", "window", or "" (the default, no progress bar)
}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}
)overlay
, reclass
, Arith-methods, Math-methodsr <- 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 DataLab