Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

math

Arithmetic, logical and general mathematical methods


Description

Standard operators and mathematical methods for computations with SpatRaster objects. Computations are local (applied on a cell by cell basis). If multiple SpatRaster objects are used, these must have the same extent and resolution. These have been implemented:

Arith: +, -, *, /, ^, %%, %/%

Compare: ==, !=, >, <, <=, >=, is.na, is.nan, is.finite, is.infinite

The terra package does not distinguish between NA (not available) and NaN (not a number). In most cases this state is represented by NaN.

Logical: !, &, |, isTRUE, isFALSE

Summary: max, min, range, prod, sum, any, all

Math: abs, sign, sqrt, ceiling, floor, trunc, cummax, cummin, cumprod, cumsum, log, log10, log2, log1p, acos, acosh, asin, asinh, atan, atanh, exp, expm1, cos, cosh, sin, sinh, tan, tanh, round, signif

The following methods have been implemented for SpatExtent: round, floor, ceil, ==, for (SpatExtent, SpatExtent): ==, +, -, and for (SpatExtent, numeric) : +, -, *, /, %%

Usage

## S4 method for signature 'SpatRaster'
log(x, base=exp(1))

Arguments

x

SpatRaster

base

a positive or complex number: the base with respect to which logarithms are computed

Value

SpatRaster or SpatExtent

seealso

ifel to convieniently combine operations and app to use mathematical functions not implemented by the package.

Examples

r1 <- rast(ncols=10, nrows=10)
v <- runif(ncell(r1))
v[10:20] <- NA
values(r1) <- v
r2 <- rast(r1)
values(r2) <- 1:ncell(r2) / ncell(r2)
r3 <- r1 + r2
r2 <- r1 / 10
r3 <- r1 * (r2 - 1 / r2)


b <- c(r1, r2, r3)
b2 <- b * 10
s <- sqrt(b2)
round(s, 1)

max(s)
max(s, na.rm=TRUE)

x <- is.na(s)

y <- which.max(s)

### SpatExtent methods
x <- ext(0.1, 2.2, 0, 3)
y <- ext(-2, 1, -2,2)
# union
x + y
# intersection
x * y

e <- x 
e
e * 2
e / 2
e + 1
e - 1

terra

Spatial Data Analysis

v1.2-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Roger Bivand [ctb] (<https://orcid.org/0000-0003-2392-6140>), Karl Forner [ctb], Jeroen Ooms [ctb] (<https://orcid.org/0000-0002-4035-0289>), Edzer Pebesma [ctb] (<https://orcid.org/0000-0001-8049-7069>)
Initial release
2021-05-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.