Learn R Programming

terra (version 0.4-3)

ifel: ifelse for SpatRaster objects

Description

This functions like ifelse but this one works for SpatRaster objects. This is "syntactic sugar", that is, it can be easier to express what is desired; but there is no real need for it, as you can achieve the same things with combinations of app, reclassify, mask, and cover.

ifel is 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