R/checks.R

Defines functions valid_pos_int valid_nonneg_int valid_int

#####INTEGER SIGN FUNCTIONS#####
# either x is an integer or is a numeric that is very close to an integer
valid_int <- function(x, eps = 1e-7) {
  if ("integer" %in% class(x)) return(TRUE)

  else if ("numeric" %in% class(x)) {
    x_int <- round(x, digits = 0)

    return(abs(x - x_int) <= eps)

  # neither numeric nor integer, can't be a valid int (per this pkg)
  } else return(FALSE)
}

# x is a non-negative integer
valid_nonneg_int <- function(x) {
  return(valid_int(x) & x >= 0)
}

# x is a positive integer
valid_pos_int <- function(x) {
  return(valid_int(x) & x > 0)
}
sbarkerclarke-phd/CoOccurR documentation built on April 5, 2024, 1:48 p.m.