From murdoch at stats.uwo.ca  Tue Feb 22 23:31:59 2005
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Tue Feb 22 23:32:12 2005
Subject: [Rd] bug? quantile() can return decreasing sample quantiles for
	increasing probabilities
In-Reply-To: <6.2.1.2.2.20050222145831.06d5c1f8@mailhost.blackmesacapital.com>
References: <6.2.1.2.2.20050220232737.06db1638@mailhost.blackmesacapital.com>
	<uk7n119r4ll8t6r5kk63psm8p1mevnmrvc@4ax.com>
	<6.2.1.2.2.20050222145831.06d5c1f8@mailhost.blackmesacapital.com>
Message-ID: <macn11hfee68ppsbagncfjqgj6i3662sc6@4ax.com>

On Tue, 22 Feb 2005 15:14:00 -0700, Tony Plate
<tplate@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 
          20% 
-0.0009041968 

Duncan Murdoch


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