[R] Why does a[which(b == c[d])] not work?

Richard A. O'Keefe ok at cs.otago.ac.nz
Wed Oct 8 23:53:58 CEST 2003


Achim Zeileis <zeileis at ci.tuwien.ac.at> wrote:
	R> x <- 406.7 + 1e-20 
	R> x
	[1] 406.7
	R> x == 406.7
	[1] TRUE
	
	that is
	  1.) `==' comparisons have a certain tolerance

No, all.equal() supports tolerance, == does not.

Consider
	> .Machine$double.eps 
	[1] 2.220446e-16

That is, the smallest relative difference that can be represented on
my (fairly typical IEEE-conforming) machine is 2 in 10 to the 16th.

1e-20/406.7 is 2.458815e-23, which is a factor of 10 million too small
a relative difference for the hardware to be able to represent.  So in

	x <- 406.7 + 1e-20

the value of x is identical to 406.7 in every bit.

	Instead of using `==' you should use a comparison with a certain 
	tolerance you can specify...
	
Such as ?all.equal




More information about the R-help mailing list