R/is.numeral.R

##' @title Can vector be coerced to numeric?
##'
##' @details
##' Checks element-wise as to whether each item in a vector of strings can be coerced to numeric.
##'
##' @param c A vector of strings.
##' @return a vector of logicals, NA where is.na(c)
##' @author Basics cribbed from \url{http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#R}
##' @author David Braze \email{davebraze@@gmail.com}
##' @export
is.numeral <- function(c) {
    retval <- suppressWarnings(!is.na(as.numeric(c)))
    retval[is.na(c)] <- NA
    retval
}

if(FALSE){
    x <- c('a', 'IX', 'twelve', '0001', '2', '3e1', '-4', '5.5', 'Inf', NA)
    data.frame(x,
               is.numeral(x),
               as.numeric(x))
}
davebraze/FDB1 documentation built on May 14, 2019, 8:59 p.m.