[R] erratic behavior of match()?

Alan Zaslavsky zaslavsk at hcp.med.harvard.edu
Fri Apr 20 02:28:47 CEST 2007


>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)

The usual solution for testing a==b with floating-point round-off error is 
abs(a-b)<tol where tol depends on the magnitude of a,b and the likely 
error.  Match is a bit trickier but you could round at some interval 
representing "close enough" numbers to force almost equal numbers to the 
same representation, e.g.

> X1=seq(0,1,len=11)
> X2=seq(0,1,len=101)
> match(X1,X2)
  [1]   1  11  21  NA  41  51  NA  71  81  91 101
> match(round(X1,2),round(X2,2))
  [1]   1  11  21  31  41  51  61  71  81  91 101

In the following case, X2 does not round "exactly" but 7-digit accuracy 
seems fine.

> X2=seq(0,1,len=31)
> match(X1,X2)
  [1]  1  4  7 NA 13 16 NA NA 25 28 31
> match(round(X1,7),round(X2,7))
  [1]  1  4  7 10 13 16 19 22 25 28 31



More information about the R-help mailing list