Skip to content

quantile

3 messages · liujb, jim holtman, (Ted Harding)

#
Hello,

I need to assign a number to each x[i], i=1:100, based on the value of x[i]
and where they are in the distribution of x[i]. For example 1 for x[4] means
x[4] is below 25%. I can obtain the quantile using quantile command, and
just loop through the 1:100 and assign the correct number. But I was just
wondering whether there are a more efficient way.

Thank you,
sincerely,
Julia
#
?quantile
?cut
0%        25%        50%        75%       100%
0.06178627 0.29216247 0.60098370 0.83899171 0.94467527
[1] [0.0618,0.292] (0.292,0.601]  (0.292,0.601]  (0.839,0.945]
[0.0618,0.292] (0.839,0.945]
 [7] (0.839,0.945]  (0.601,0.839]  (0.601,0.839]  [0.0618,0.292]
Levels: [0.0618,0.292] (0.292,0.601] (0.601,0.839] (0.839,0.945]
[1] 1 2 2 4 1 4 4 3 3 1
[1] 0.26550866 0.37212390 0.57285336 0.90820779 0.20168193 0.89838968
0.94467527 0.66079779 0.62911404
[10] 0.06178627

        
On Mon, Sep 29, 2008 at 4:09 PM, liujb <liujulia7 at yahoo.com> wrote:

  
    
#
On 29-Sep-08 20:09:14, liujb wrote:
Well, you can certainly do it with a much shorter loop!

set.seed(31425)
x   <- rnorm(13)
x.v <- numeric(length(x))
ix  <- order(x)
Q <- quantile(x)
for(i in (5:2)){ x.v[x<=Q[i]] <- (i-1) }

cbind(x,x.v, x[ix],x.v[ix])
               x x.v             
 [1,] -0.7565336   2 -1.7045077 1
 [2,] -0.3287683   2 -1.0693801 1
 [3,] -1.7045077   1 -1.0671752 1
 [4,]  0.7259883   4 -0.9718954 1
 [5,]  0.6174724   3 -0.7565336 2
 [6,] -1.0693801   1 -0.3668566 2
 [7,]  1.9826596   4 -0.3287683 2
 [8,] -0.9718954   1  0.2491123 3
 [9,] -1.0671752   1  0.4733287 3
[10,] -0.3668566   2  0.6174724 3
[11,]  0.2491123   3  0.7259883 4
[12,]  0.4733287   3  1.9826596 4
[13,]  2.2095536   4  2.2095536 4

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 29-Sep-08                                       Time: 22:33:33
------------------------------ XFMail ------------------------------