[R] Deciles and R

Marc Schwartz MSchwartz at MedAnalytics.com
Thu Apr 21 17:41:15 CEST 2005


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




More information about the R-help mailing list