[R] Relative ranking by a factor

jiho.han
Thu, 22 Feb 2007 03:18:50 -0000

Hi, R experts-

I am trying to create a function that returns a quintile (or decile) ranking
of a variable controlled by a factor. For example, suppose there are 10
people each in class A (1st graders) and class B (2nd graders). If you want
to calculate quintile rank for each class, how would you do that?

Right now, I create the following function and use it with "by"
function.
This way works ok except that it's getting slow when the data.frame is
really really large.

rank.group = function (x, ng) {
        y = quantile(x, seq(0,1,length.out=ng+1), na.rm=T)
        z = cut( x, y, include.lowest = T); z = as.numeric(z)
        return(z) }

Hope there's a faster way to rank the variable as in SAS.
Thanks for your help.



--
View this message in context:
http://www.nabble.com/Relative-ranking-by-a-factor-tf3270737.html#a9093712
Sent from the R help mailing list archive at Nabble.com.