isMonotonic: Tests to see if a sequence is ascending or descending

isMonotonicR Documentation

Tests to see if a sequence is ascending or descending

Description

These functions take a vector and check to see if it is in increasing or decreasing order. The function isMonotonic returns true if the sequence is either increasing (nondecreasing) or decreasing (nonincreasing), with the parenthesized values used if strict is false. For isMonotonic an attribute of the return value tell the direction of the series.

Usage

isMonotonic(vec, strict = TRUE)
isIncreasing(vec)
isNondecreasing(vec)
isDecreasing(vec)
isNonincreasing(vec)

Arguments

vec

A vector of sortable objects (i.e., ones for which the comparison operators are defined. The vector should have length of at least 2.

strict

A logical value. If true, then the function will check for strict monotonicity.

Details

These function use the following definitions:

Increasing.

vec[1] < vec[2] < ... < vec[k].

Nondecreasing.

vec[1] <= vec[2] <= ... <= vec[k].

Decreasing.

vec[1] > vec[2] > ... > vec[k].

Nonincreasing.

vec[1] >= vec[2] >= ... >= vec[k].

A sequence is monotonic if it is either nondecreasing or nonincreasing. It is strictly monotonic if it is either increasing or decreasing. Note that the function isMonotonic by default checks for strict monotonicity; which is usually more useful in the context of Bayesian networks.

It is often of interest to check if the direction is increasing or decreasing. The function isMonotonic returns an attribute "direction" which is positive for increasing series and negative for decreasing series.

Value

The functions isIncreasing, isNondecreasing, isDecreasing, and isNonincreasing a logical according to whether or not the corresponding condition holds for vec.

The function isMonotonic returns a logical value with an additional attribute "direction". The expression attr(isMonotonic(vec),"direction" will be positive if the series is increasing and negative if it is decreasing. If all values are equal, and strict is false, the direction will be 0.

If vec has length less than 2, then NA will be returned.

Note

Because isMonotonic returns an extra attribute, isTRUE(isMonotonic(vec) will always be false. The expression isTRUE(c(isMonotonic(vec))) is likely to be more useful.

Author(s)

Russell Almond

See Also

sort, order

Examples


isIncreasing(1:3)
isNondecreasing(c(1,1,3))
isDecreasing(3:1)
isNonincreasing(c(3,1,1))

isMonotonic(1:3)
isMonotonic(c(1,1,3))  #FALSE
isMonotonic(c(1,1,3),strict=FALSE)
isMonotonic(3:1)
isMonotonic(c(3,3,1),strict=FALSE)
isMonotonic(c(0,0,0)) #FALSE
isMonotonic(c(0,0,0),FALSE) #TRUE
isMonotonic(c(3,0,3)) # FALSE


ralmond/CPTtools documentation built on Dec. 27, 2024, 7:15 a.m.