[R] Problems with rank()

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 28 13:26:07 CET 2001


On Wed, 28 Nov 2001, Stephane Hess wrote:

> If you enter the following values for x and y:
> x: 2.2 3.7 2.1 0.4 2.8 0.3 0.4 1.4 5.4 6.0
> y: 6.0 8.1 1.8 1.3 5.2 0.6 1.0 1.9 6.8 6.5
> and do rank(abs(y-x)),
> you should get two ties, one at 0.3 and one at 0.5.
> R, and S-Plus5 by that matter recognise the tie at 0.5 and give
> it rank 3.5, but gives one of the two 0.3 values rank 1 and one of
> them 2, whereas they should boh be 1.5.
> Any suggestions?
> when you declare a vector with the positive differences manually,
> it ranks them in the correct order.
> if you use rank(sqrt((y-x)^2), it produces the same mistake...
>
> Stephane Hess,
> Statslab Cambridge University

It's called rounding error.  You don't get 0.3: my machine gives

> print(sort(abs(x-y)), digits=17)
 [1] 0.29999999999999999 0.30000000000000004 0.50000000000000000
 [4] 0.50000000000000000 0.59999999999999998 0.90000000000000002
 [7] 1.39999999999999947 2.40000000000000036 3.79999999999999982
[10] 4.39999999999999947

0.3 is not exactly representable on machines with IEC 60559 (aka IEEE 754)
arithmetic, but 0.5 is a binary fraction.

It's always worth looking at what you've done to full machine precision!
Try round(abs(x-y), 1), for example.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list