[R] as(, "numeric") vs as.numeric()

Hervé Pagès hpages at fredhutch.org
Mon Feb 1 23:18:54 CET 2016


On 02/01/2016 07:33 AM, Duncan Murdoch wrote:
> On 01/02/2016 10:00 AM, Erik Wright wrote:
>> Dear Frank,
>>
>> Thank you for the quick response.
>>
>> I am familiar with the tradeoffs between integers and doubles.
>> However, I do not believe this answers my question.
>>
>> If you look at the help information for the as() function it says:
>> "as(x, "numeric") uses the existing as.numeric function."  But clearly
>> the result is different in each case.
>
> Since is.numeric(1:10) and is(1:10, "numeric") are both true, the as()
> function eventually bails out and does nothing.

But it should. Because as() has an extra argument 'strict' that is TRUE
by default. From the man page for as():

   strict: logical flag.  If ‘TRUE’, the returned object must be
           strictly from the target class (unless that class is a
           virtual class, in which case the object will be from the
           closest actual class, in particular the original object, if
           that class extends the virtual class directly).

           If ‘strict = FALSE’, any simple extension of the target class
           will be returned, without further change.  A simple extension
           is, roughly, one that just adds slots to an existing class.

So the current behavior is clearly a bug, has been reported several
times, and is known from the R-core folks:

   https://stat.ethz.ch/pipermail/r-devel/2015-December/072079.html

FWIW this bug is actually related to this other bug:

   x <- 1:10
   class(x)
   # [1] "integer"

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

Cheers,
H.


>  So yes, as(x,
> "numeric") uses as.numeric() when it needs to coerce, but not when no
> coercion is necessary.   The docs could perhaps add this condition.
>
> Duncan Murdovh
>>
>> If the help for as() is correct, then as(1:10, "numeric") should also
>> return doubles, and the second argument is not ignored.
>>
>> Erik
>>
>>
>> > On Feb 1, 2016, at 8:16 AM, Franklin Bretschneider
>> <bretschr at xs4all.nl> wrote:
>> >
>> > Dear Erik Wright,
>> >
>> >
>> > Re:
>> >
>> >> Could someone please explain this R behavior to me:
>> >>
>> >>> typeof(as.numeric(1:10))
>> >> [1] "double"
>> >>> typeof(as(1:10, "numeric"))
>> >> [1] "integer"
>> >>
>> >> I expected "double" in both cases.  In the help for the "as"
>> function it says:
>> >>
>> >> "Methods are pre-defined for coercing any object to one of the
>> basic datatypes. For example, as(x, "numeric") uses the existing
>> as.numeric function."
>> >
>> >
>> > This happens because 1:10 yields only integers, and so can be stored
>> cheap,
>> > whereas as.numeric() actually means: as.double.
>> > The "numeric" in the second line is an unused argument.
>> >
>> > Best regards,
>> >
>> > Frank
>> > ---
>> >
>> >
>> >
>> >
>> > Franklin Bretschneider
>> > Dept of Biology
>> > Utrecht University
>> > bretschr at xs4all.nl
>> >
>> >
>> >
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fredhutch.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-help mailing list