isConstant {IRanges}R Documentation

Test if an atomic vector or array is constant

Description

Generic function to test if an atomic vector or array is constant or not. Currently only methods for vectors or arrays of type integer or double are implemented.

Usage

isConstant(x)

Arguments

x

An atomic vector or array.

Details

Vectors of length 0 or 1 are always considered to be constant.

Value

A single logical i.e. TRUE, FALSE or NA.

Author(s)

H. Pages

See Also

duplicated, unique, all.equal, NA, is.finite

Examples

  ## ---------------------------------------------------------------------
  ## A. 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_))


  ## ---------------------------------------------------------------------
  ## B. 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!)

[Package IRanges version 1.14.4 Index]