Josh O'Brien
2015-Dec-04 17:56 UTC
[Rd] For integer vectors, `as(x, "numeric")` has no effect.
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")`?
Benjamin Tyner
2015-Dec-07 23:37 UTC
[Rd] For integer vectors, `as(x, "numeric")` has no effect.
Perhaps it is not that surprising, given that > mode(1L) [1] "numeric" and > is.numeric(1L) [1] TRUE On the other hand, this is curious, to say the least: > is.double(as(1L, "double")) [1] FALSE> 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")`?
John Chambers
2015-Dec-08 00:05 UTC
[Rd] For integer vectors, `as(x, "numeric")` has no effect.
We do need an explicit method here, I think. The issue is that as() uses methods for the generic function coerce() but cannot use inheritance in the usual way (if it did, you would be immediately back with no change, since "integer" inherits from "numeric"). Copying in the general method for coercing to "numeric" as an explicit method for "integer" gives the expected result:> setMethod("coerce", c("integer", "numeric"), getMethod("coerce", c("ANY", "numeric")))[1] "coerce"> typeof(as(1L, "numeric"))[1] "double" Seems like a reasonable addition to the code, unless someone sees a problem. John On Dec 7, 2015, at 3:37 PM, Benjamin Tyner <btyner at gmail.com> wrote:> Perhaps it is not that surprising, given that > > > mode(1L) > [1] "numeric" > > and > > > is.numeric(1L) > [1] TRUE > > On the other hand, this is curious, to say the least: > > > is.double(as(1L, "double")) > [1] FALSE > >> 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")`? > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Apparently Analagous Threads
- For integer vectors, `as(x, "numeric")` has no effect.
- For integer vectors, `as(x, "numeric")` has no effect.
- For integer vectors, `as(x, "numeric")` has no effect.
- For integer vectors, `as(x, "numeric")` has no effect.
- For integer vectors, `as(x, "numeric")` has no effect.