Skip to content
Prev 360016 / 398503 Next

[FORGED] Re: identical() versus sapply()

On 11/04/2016 8:25 PM, Paulson, Ariel wrote:
The issue here is that R has grown.  The as() function is newer than the 
as.numeric() function, it's part of the methods package.  It is a much 
more complicated thing, and there are cases where they differ.

In this case, the problem is that is(1L, "numeric") evaluates to TRUE, 
and nobody has written a coerce method that specifically converts 
"integer" to "numeric".  So the as() function defaults to doing nothing.
It takes a while to do nothing, approximately 360 times longer than 
as.numeric() takes to actually do the conversion:

 > microbenchmark(as.numeric(1L), as(1L, "numeric"))
Unit: nanoseconds
               expr   min    lq      mean  median       uq     max neval
     as.numeric(1L)   133   210    516.92   273.5    409.5    9444   100
  as(1L, "numeric") 51464 64501 119294.31 99768.5 138321.0 1313669   100

R performance is not always simple and easy to predict, but I think 
anyone who had experience with R would never use as(x, "numeric").  So 
this just isn't a problem worth fixing.

Now, you might object that the documentation claims they are equivalent, 
but it certainly doesn't.  The documentation aims to be accurate, not 
necessarily clear.

Duncan Murdoch

Thread (30 messages)

Paulson, Ariel identical() versus sapply() Apr 8 Jeff Newmiller identical() versus sapply() Apr 8 Rolf Turner identical() versus sapply() Apr 9 Duncan Murdoch identical() versus sapply() Apr 9 Paulson, Ariel identical() versus sapply() Apr 11 Bert Gunter identical() versus sapply() Apr 11 Jeff Newmiller identical() versus sapply() Apr 11 Paulson, Ariel identical() versus sapply() Apr 11 William Dunlap identical() versus sapply() Apr 11 Duncan Murdoch identical() versus sapply() Apr 11 Paulson, Ariel identical() versus sapply() Apr 11 Paulson, Ariel identical() versus sapply() Apr 11 Bert Gunter identical() versus sapply() Apr 11 Duncan Murdoch identical() versus sapply() Apr 11 Rolf Turner identical() versus sapply() Apr 11 Rolf Turner identical() versus sapply() Apr 11 John Kane identical() versus sapply() Apr 12 John Kane identical() versus sapply() Apr 12 Duncan Murdoch identical() versus sapply() Apr 12 John C Nash Documentation: Was -- identical() versus sapply() Apr 12 Michael Dewey identical() versus sapply() Apr 12 Duncan Murdoch Documentation: Was -- identical() versus sapply() Apr 12 Bert Gunter Documentation: Was -- identical() versus sapply() Apr 12 John C Nash Documentation: Was -- identical() versus sapply() Apr 12 Sarah Goslee Documentation: Was -- identical() versus sapply() Apr 12 David Winsemius Documentation: Was -- identical() versus sapply() Apr 12 Duncan Murdoch Documentation: Was -- identical() versus sapply() Apr 12 Duncan Murdoch Documentation: Was -- identical() versus sapply() Apr 12 William Michels Documentation: Was -- identical() versus sapply() Apr 12 John C Nash Documentation: Was -- identical() versus sapply() Apr 12