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

From: Duncan Murdoch <>
Date: Wed 23 Feb 2005 - 09:31:59 EST

On Tue, 22 Feb 2005 15:14:00 -0700, Tony Plate <> 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 mailing list Received on Wed Feb 23 08:43:42 2005

This archive was generated by hypermail 2.1.8 : Wed 23 Feb 2005 - 11:49:16 EST