Learn R Programming

terra (version 0.7-11)

ifel: ifelse for SpatRaster objects

Description

This functions like ifelse but this one works for SpatRaster objects. This method allows for a concise approach to what can otherwise be achieved with a combination of classify, mask, and cover.

ifel is the R equivalent to the Con method in ArcGIS (arcpy).

Usage

# S4 method for SpatRaster
ifel(test, yes, no, filename="", overwrite=FALSE, wopt=list(), ...)

Arguments

test

SpatRaster

yes

SpatRaster or numeric

no

SpatRaster or numeric

filename

character. Output filename. Optional

overwrite

logical. If TRUE, filename is overwritten

wopt

list. Options for writing files as in writeRaster

...

additional arguments. None implemented

Value

SpatRaster

Examples

Run this code
# NOT RUN {
r <- rast(nrows=5, ncols=5)
values(r) <- -10:14

x <- ifel(r > 1, 1, r)
# same as 
a <- classify(r, cbind(1, Inf, 1))
b <- app(r, fun=function(i) {i[i > 1] <- 1; i})
d <- clamp(r, -Inf, 1)

y <- ifel(r > 1, 1, ifel(r < -1, -1, r))

z <- ifel(r > -2 & r < 2, 100, 0)

k <- ifel(r > 0, r+10, ifel(r < 0, r-10, 3))
# }

Run the code above in your browser using DataLab