R/can-be-num.R

Defines functions str_can_be_numeric

Documented in str_can_be_numeric

#' Check if a string could be considered as numeric.
#'
#' After padding is removed, could the input string be considered to be numeric,
#' i.e. could it be coerced to numeric. This function is vectorized over its one
#' argument.
#'
#' @inheritParams str_after_nth
#'
#' @return A logical vector.
#'
#' @examples
#' str_can_be_numeric("3")
#' str_can_be_numeric("5 ")
#' str_can_be_numeric(c("1a", "abc"))
#' @family type converters
#' @export
str_can_be_numeric <- function(string) {
  checkmate::assert(
    checkmate::check_character(string),
    checkmate::check_numeric(string)
  )
  !is.na(suppressWarnings(as.numeric(string)))
}

Try the strex package in your browser

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

strex documentation built on Nov. 2, 2023, 6:04 p.m.