## ---------------------------------------------------------------------
## A. isSorted() and isStrictlySorted()
## ---------------------------------------------------------------------
x <- 1:10
isSorted(x) # TRUE
isSorted(-x) # FALSE
isSorted(rev(x)) # FALSE
isSorted(-rev(x)) # TRUE
isStrictlySorted(x) # TRUE
x2 <- rep(x, each=2)
isSorted(x2) # TRUE
isStrictlySorted(x2) # FALSE
## ---------------------------------------------------------------------
## B. "isConstant" METHOD FOR integer VECTORS
## ---------------------------------------------------------------------
## On a vector with no NAs:
stopifnot(isConstant(rep(-29L, 10000)))
## On a vector with NAs:
stopifnot(!isConstant(c(0L, NA, -29L)))
stopifnot(is.na(isConstant(c(-29L, -29L, NA))))
## On a vector of length <= 1:
stopifnot(isConstant(NA_integer_))
## ---------------------------------------------------------------------
## C. "isConstant" METHOD FOR numeric VECTORS
## ---------------------------------------------------------------------
## This method does its best to handle rounding errors and special
## values NA, NaN, Inf and -Inf in a way that "makes sense".
## Below we only illustrate handling of rounding errors.
## Here values in 'x' are "conceptually" the same:
x <- c(11/3,
2/3 + 4/3 + 5/3,
50 + 11/3 - 50,
7.00001 - 1000003/300000)
## However, due to machine rounding errors, they are not *strictly*
## equal:
duplicated(x)
unique(x)
## only *nearly* equal:
all.equal(x, rep(11/3, 4)) # TRUE
## 'isConstant(x)' uses 'all.equal()' internally to decide whether
## the values in 'x' are all the same or not:
stopifnot(isConstant(x))
## This is not perfect though:
isConstant((x - 11/3) * 1e8) # FALSE on Intel Pentium paltforms
# (but this is highly machine dependent!)
Run the code above in your browser using DataLab