RE: [R] Deciles and R

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Fri 22 Apr 2005 - 01:46:37 EST


On 21-Apr-05 Nuno Soares wrote:
> Hi everyone,
>
> I'm a new R user (if this is a really basic question, please
> do excuse me...) and I'm having some questions regarding a
> deciles problem.
>
> I have a variable which I need to categorize according to
> its deciles (X).
> However, this categorization should be made into another
> variable (call it NewVar).
>
> Ex. for the quartiles case (just for the sake of exposition,
> since I need deciles...), I would like to be able to generate
> the NewVar variable based on the quantiles of X:
>
> X NewVar
> 1 1
> 6 2
> 2 1
> 4 2
> 3 1
> 5 2
> 12 4
> 9 3
> 8 3
> 10 4
> 11 4
> 7 3
>
> Is there a function or a way of doing this automatically?
> I've searched the help files but found no solution to this problem...

Well, the solution would not leap to the eye unless you knew what to look for!.

However, "cut" is what you need at the heart of this. Using your example,

  X<-c(1,6,2,4,3,5,12,9,8,10,11,7)

  NewVar<-cut(X,quantile(X,(0:4)/4),include.lowest=TRUE)   cbind(X,NewVar)

       X NewVar
 [1,]  1      1
 [2,]  6      2
 [3,]  2      1
 [4,]  4      2
 [5,]  3      1
 [6,]  5      2
 [7,] 12      4
 [8,]  9      3
 [9,]  8      3
[10,] 10      4
[11,] 11      4
[12,]  7      3

Just change (0:4)/4 to (0:10)/10 for deciles.

Hoping this helps,
Ted.



E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 21-Apr-05                                       Time: 16:46:37
------------------------------ XFMail ------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Apr 22 02:02:26 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:21 EST