R/coerce.R

Defines functions asInteger

Documented in asInteger

#' @title Coerce functions
#'
#' @description
#'
#' - `asInteger`: convert `x` to an `integer` and throw an error if `x` is not
#'   a `numeric`.
#'
#' @param x input argument.
#'
#' @author Johannes Rainer
#'
#' @family coerce functions
#' @name coerce
#' @aliases asInteger
#' @export
#' @examples
#' ## Convert numeric to integer
#' asInteger(3.4)
#'
#' asInteger(3)
asInteger <- function(x) {
    if (is.numeric(x))
        as.integer(x)
    else {
        arg <- deparse(substitute(x))
        stop("Argument ", arg, " should be a numeric or integer", call. = FALSE)
    }
}

Try the MsCoreUtils package in your browser

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

MsCoreUtils documentation built on Nov. 8, 2020, 10:59 p.m.