Skip to content
Prev 86983 / 398506 Next

Ranking within factor subgroups

It might help to give a simple reproducible example in the future. For
example

 df <- cbind.data.frame( date=rep( 1:5, each=100 ), A=rpois(500, 100),
                         B=rpois(500, 50), C=rpois(500, 30) )

might generate something like

	    date   A  B  C
	  1    1  93 51 32
	  2    1  95 51 30
	  3    1 102 59 28
	  4    1 105 52 32
	  5    1 105 53 26
	  6    1  99 59 37
	...    . ... .. ..
	495    5 100 57 19
	496    5  96 47 44
	497    5 111 56 35
	498    5 105 49 23
	499    5 105 61 30
	500    5  92 53 32

Here is my proposed solution. Can you double check with your existing
functions to see if they are correct.

   decile.fn <- function(x, nbreaks=10){
     br     <- quantile( x, seq(0, 1, len=nbreaks+1), na.rm=T )
     br[1]  <- -Inf
     return( cut(x, br, labels=F) )
   }

   out <- apply( df[ ,c("A", "B", "C")], 2,
                 function(v) unlist( tapply( v, df$date, decile.fn ) ) )

   rownames(out) <- rownames(df)
   out <- cbind(df$date, out)

Regards, Adai
On Tue, 2006-02-21 at 21:44 -0500, maneesh deshpande wrote: