R/as.numericSilent.R

Defines functions as.numericSilent

Documented in as.numericSilent

##' Convert any vector to numeric, if possible
##'
##' @export
##' @param x vector of any type
##'
##' @return If \code{as.numeric(x)} produces an error or warning, \code{x} is
##' returned unchanged.  Otherwise, \code{as.numeric(x)} is returned.
##'
##' @author Landon Sego
##'
##' @seealso \code{\link{as.numeric}}
##'
##' @keywords misc
##'
##' @examples
##' as.numericSilent(c("this","that"))
##' as.numericSilent(c("2893.9","9423.48"))
##' as.numericSilent(c("392.1", "that"))

as.numericSilent <- function(x) {

  # Set warnings to errors
  op <- options(warn = 2)

  # If an error results, the data aren't numeric
  if (class(x.num <- try(as.numeric(x), silent = TRUE)) == "try-error") {
    x.num <- x
  }

  # Restore the previous setting
  options(op)

  return(x.num)

} # as.numericSilent()

Try the Smisc package in your browser

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

Smisc documentation built on May 2, 2019, 2:46 a.m.