[Rd] as(1:4, "numeric") versus as.numeric(1:4, "numeric")

John Chambers jmc at r-project.org
Thu Apr 1 01:52:51 CEST 2010


The example is confusing and debatable, but not an obvious bug.  And 
your presentation of it is the cause of much of the confusion 
(unintentionally I'm sure).

First, slipping from the as() function to methods for the coerce() 
function might surprise a less experienced user.  And in fact, that is 
the point here.  If you look at the as() function, it jumps through 
several hoops and in particular selects a method from coerce in such a 
way as NOT to use inheritance on the from= argument.  (I think this 
makes sense in this case).  So I would assert that your selectMethod() 
output below came from a different session than the as(1:4, "numeric").

Starting from a clean session with R 2.10.1:

 > class(as(1:4,"numeric"))
[1] "integer"
 > selectMethod("coerce", c("integer","numeric"))
Method Definition:

function (from, to = "numeric", strict = TRUE)
if (strict) {
     class(from) <- "numeric"
     from
} else from
<environment: namespace:methods>

Signatures:
         from      to
target  "integer" "numeric"
defined "integer" "numeric"

Note, no call to as.numeric().  In a session without a previous call to 
as(), your selectMethod() call triggered a standard inherited method 
selection.  And if you had then gone on to as(), the result would have 
been different.

In a different clean session:


 > getMethod("coerce", c("integer", "numeric"))
Error in getMethod("coerce", c("integer", "numeric")) :
   No method found for function "coerce" and signature integer, numeric
 > selectMethod("coerce", c("integer", "numeric"))
Method Definition:

function (from, to, strict = TRUE)
{
     value <- as.numeric(from)
     if (strict)
         attributes(value) <- NULL
     value
}
<environment: namespace:methods>

Signatures:
         from      to
target  "integer" "numeric"
defined "ANY"     "numeric"
 > class(as(1:4,"numeric"))
[1] "numeric"

No argument about this being confusing.  Perhaps one should prohibit 
standard selectMethod() on coerce() but that seems a bit arcane to 
thwart folks like you!

Suggested improvements for the current implementation are welcome, so 
long as they consider the best definition of as() in the general sense.

Regards,
   John

On 3/31/10 3:52 PM, Hervé Pagès wrote:
> Hi,
>
> > class(as(1:4, "numeric"))
>   [1] "integer"
>
> Surprising but an explanation could be that an integer
> vector being a particular case of numeric vector, this
> coercion has nothing to do because 1:4 is already numeric.
> And indeed:
>
> > is.numeric(1:4)
>   [1] TRUE
> > is.numeric(as(1:4, "numeric"))
>   [1] TRUE
>
> However, 'as(1:4, "numeric")' is inconsistent with
>
> > class(as.numeric(1:4))
>   [1] "numeric"
>
> And, even more confusing, if you look at the coerce,ANY,numeric
> method:
>
> > selectMethod("coerce", c("integer", "numeric"))
>   Method Definition:
>
>   function (from, to, strict = TRUE)
>   {
>     value <- as.numeric(from)
>     if (strict)
>         attributes(value) <- NULL
>     value
>   }
> <environment: namespace:methods>
>
>   Signatures:
>           from      to
>   target  "integer" "numeric"
>   defined "ANY"     "numeric"
>
> it calls as.numeric()!
>
> So how can 'as(1:4, "numeric")' not return the same thing as
> 'as.numeric(1:4)' looks like a mystery to me. Could it be
> conceivable that I found a bug?
>
> Cheers,
> H.
>
>
> > sessionInfo()
> R version 2.11.0 Under development (unstable) (2010-03-15 r51282)
> x86_64-unknown-linux-gnu
>
> locale:
>  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
>  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
>  [5] LC_MONETARY=C              LC_MESSAGES=en_CA.UTF-8
>  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C
>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
>



More information about the R-devel mailing list