[Rd] For integer vectors, `as(x, "numeric")` has no effect.

Josh O'Brien joshmobrien at gmail.com
Fri Dec 4 18:56:13 CET 2015


Here's the surprising behavior:

   x <- 1L
   xx <- as(x, "numeric")
   class(xx)
   ## [1] "integer"

It occurs because the call to `as(x, "numeric")` dispatches the coerce
S4 method for the signature `c("integer", "numeric")`, whose body is
copied in below.

    function (from, to = "numeric", strict = TRUE)
    if (strict) {
        class(from) <- "numeric"
        from
    } else from

This in turn does nothing, even when strict=TRUE, because that
assignment to class "numeric" has no effect:

    x <- 10L
    class(x) <- "numeric"
    class(x)
    [1] "integer"

Is this the desired behavior for `as(x, "numeric")`?



More information about the R-devel mailing list