R/util_is_integer.R

Defines functions util_is_integer

Documented in util_is_integer

#' Check for integer values
#'
#' This function checks if a variable is integer.
#'
#' @param x the object to test
#' @param tol precision of the detection. Values deviating more than `tol` from
#'            their closest integer value will not be deemed integer.
#'
#' @return `TRUE` or `FALSE`
#' @seealso is.integer
#'
#' Copied from the documentation of \link[base]{is.integer}
#'
#' \link[base]{is.integer} detects, if the storage mode of an R-object is
#' integer. Usually, users want to know, if the values are integer. As suggested
#' by \link[base]{is.integer}'s documentation, `is.wholenumber` does so.
#'
util_is_integer <- function(x, tol = .Machine$double.eps^0.5) {
  if (is.numeric(x)) {
    r <- abs(x - round(x)) < tol & !is.nan(x)
    # & x <= .Machine$integer.max & this would return, whether x can be stored as an integer.
    #   x >= - .Machine$integer.max
  } else {
    r <- rep(FALSE, length(x))
  }
  r[is.na(r)] <- TRUE # NA is not not an integer
  r
}

Try the dataquieR package in your browser

Any scripts or data that you put into this service are public.

dataquieR documentation built on July 26, 2023, 6:10 p.m.