R/as_like.R

Defines functions is.integer_like is.numeric_like

Documented in is.integer_like is.numeric_like

#' Can Vector be Coerced to Numeric
#'
#' @param x numeric or character vector
#'
#' @return logical - True if is.numeric or can be coerced to a numeric
#' @export
#'
#' @examples
is.numeric_like <- function(x) {

  ret <- FALSE
  if(is.numeric(x)) {
    return(TRUE)
  } else if(is.character(x)){
    tryCatch(
      {
        x1 <- as.numeric(x)
        return(!is.na(x))
      },
      warning = function(cond) {
        return(FALSE)
      }

    )


  } else {
    return(FALSE)
  }
  ret

}





#' Can Vector be Coerced to Integer
#'
#' @param x integer, numeric, or character vector
#'
#' @return logical - True if is.integer or can be coerced to an integer
#' @export
#'
#' @examples
is.integer_like <- function(x) {

  ret <- FALSE
  if(is.numeric(x)) {
    return(TRUE)
  } else if(is.character(x)){
    tryCatch(
      {
        x1 <- as.integer(x)
        return(!is.na(x1))
      },
      warning = function(cond) {
        return(FALSE)
      }

    )


  } else {
    return(FALSE)
  }
  ret

}
JahNorr/orrr documentation built on Jan. 29, 2025, 5:11 p.m.