[R] erratic behavior of match()?

Douglas Bates bates at stat.wisc.edu
Thu Apr 19 04:08:56 CEST 2007


On 4/18/07, Bernhard Klingenberg <Bernhard.Klingenberg at williams.edu> wrote:
> Consider the code:
>
> x <- seq(0,1,0.2)
> y <- seq(0,1,0.01)
> cbind(match(y,x),y)
>
> which, surprisingly, doesn't show a match at 0.6! (It gives correct
> matches at 0, 0.2, 0.4, 0.8 and 1, though)
>
> In addition,
>
> x[4]==y[61]
>
> yields FALSE. (but x[5]==y[81], the one for 0.8,  yields TRUE)
>
> Is this a consequence of machine error or something else?
> Could this be overcome? (It works correctly when integers are used in
> the sequences as well as in many other circumstances)

See the R FAQ - question 7.31  It's a basic property of floating point
arithmetic.



More information about the R-help mailing list