[R] Why does match() treat NaN's as compables; Bug or Feature?

Jason Thorpe jdthorpe at gmail.com
Sat Feb 27 23:52:29 CET 2016


For some reason `match()` treats `NaN`'s as comparables by default:

> x <- c(1,2,3,NaN,4,5)
> match(x,x)
[1] 1 2 3 4 5 6

which I can override when using `match()` directly:

> match(x,x,incomparables=NaN)
[1]  1  2  3 NA  5  6

but not necessarily when calling a function that uses `match()` internally:

> stats::ecdf(x)(x)
[1] 0.2 0.4 0.6 0.8 0.8 1.0

Obviously there are workarounds for any given scenario, but the bigger
problem is that this behavior causes difficult to discover bugs.  For
example, the behavior of stats::ecdf is definitely a bug introduced by it's
use of `match()` (unless you think NaN == 4 is correct).

Is there a good reason that NaN's are treated as comparables by match(), or
his this a bug?

For reference, I'm using R version 3.2.3

-Jason

	[[alternative HTML version deleted]]



More information about the R-help mailing list