[Rd] bug? quantile() can return decreasing sample quantiles for increasing probabilities

Duncan Murdoch murdoch at stats.uwo.ca
Tue Feb 22 23:31:59 CET 2005

On Tue, 22 Feb 2005 15:14:00 -0700, Tony Plate
<tplate at blackmesacapital.com> wrote :

>Thanks for the diagnosis.
>The reason I came across this was that I use both S-PLUS and R and I often 
>use the results of quantile() as the breaks for cut().  In S-PLUS, cut() 
>stops with an error if breaks has any decreasing values.  Thus this example 
>caused an S-PLUS function to unexpectedly stop with an error.   However, 
>cut() in R behaves differently: it sorts its breaks and thus does not 
>object to decreasing values in breaks.  Another difference is that cut() in 
>R stops with an error if any breaks are duplicated, which, I guess, means 
>that in R I should use findInterval() instead of cut() for this 
>task.  Except that findInterval() in R stops with an error if its breaks 
>are unsorted...
> > findInterval(x2, quantile(x2, (0:5)/5))
>Error in findInterval(x2, quantile(x2, (0:5)/5)) :
>         'vec' must be sorted non-decreasingly

I guess you'll just have to use sort(quantile(...)).  It makes the
labels look sort of funny, but is hopefully harmless:

> x <- rep(-0.00090419678460984, 602)
> sort(quantile(x, 0:5/5))
           0%           40%           60%           80%          100% 
-0.0009041968 -0.0009041968 -0.0009041968 -0.0009041968 -0.0009041968 

Duncan Murdoch

More information about the R-devel mailing list