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... Regards, Nuno
On Thu, 2005-04-21 at 16:06 +0100, 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...How about this: x <- c(1, 6, 2, 4, 3, 5, 12, 9, 8, 10, 11, 7)> cbind(x, NewVar = cut(x, 4, labels = 1:4))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 See ?cut for more information. HTH, Marc Schwartz
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 at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 21-Apr-05 Time: 16:46:37 ------------------------------ XFMail ------------------------------