all.equal

Thomas Lumley thomas@biostat.washington.edu
Wed, 13 May 1998 13:01:33 -0700 (PDT)


On Wed, 13 May 1998, Paul Gilbert wrote:

> I need the function all.equal which is in Splus but not yet in R. Below is a
> first cut. Comments would be appreciated.
> 

I think it should return T/F rather than text.  You could have the reason
returned as an attribute or printed as a warning().

Some slight fuzziness in equality for real numbers might make this
function more useful (perhaps it should be optional/settable
eg all.equal(a,b,epsilon=100*.Machine()$double.eps) might use

	d<-a-b	
	all(abs(d)<epsilon)
or
	all(abs(d)/(abs(a)+abs(b))<epsilon)

since rounding error can creep in even with highly accurate computations.
For example, the survival4 library Examples suite has things which are
all.equal() in S-PLUS but differ by perhaps 1e-16 or so in R.

The test for equal values currently uses recycling for objects of
different lengths, so 1:5 and rep(1:5,2) are all.equal(). You would
probably want to insert a test on lengths.

Also, the current version crashes if fed objects whose components are not
comparable 
	eg a<-lm(y~x); b<-a; all.equal(a,b) gives
	Error: comparison is possible only for vector types
I don't know if this is right or not.

	-thomas


Thomas Lumley
------------------------
Biostatistics		
Uni of Washington	
Box 357232		
Seattle WA 98195-7232	
------------------------


> 
> all.equal <- function(obj1, obj2, ...)
>   {if(mode(obj1)      != mode(obj2) )      return("modes differ.")
>    if(class(obj1)     != class(obj2))      return("classes differ.")
>    if(! all(unlist(attributes(obj1)) == unlist(attributes(obj2))))
>                                            return("attributes differ.")
>    if(! all(unlist(obj1) == unlist(obj2))) return("values differ.")
>    T
>   }
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._