[R] Partition into quantiles

Alberto Monteiro albmont at centroin.com.br
Thu Oct 5 15:58:29 CEST 2006


Sorry, folks. Thanks for the help, but none of the suggestions 
worked quite as I wanted :-/

So I wrote the code. It seems to work:

gera_particao <- function(x, n) {
  y <- sort(x)
  icut <- as.integer(seq(1, length(x)+1, length = n + 1))
  icut <- icut[c(-(n+1))]
  ycut <- y[icut]
  for (i in 1:length(x))
    xpart[i] <- sum(x[i] >= ycut)
  return(xpart)
}

First, x is sorted to y. Then, I select the minima y
of each of the n segments. Then, an ugly and slow loop,
counts for each x how many ycut lie below them.

> x <- runif(12)
> x
 [1] 0.2971455266 0.6112766485 0.5571073645 0.5886481798 0.7499023860
 [6] 0.1681732289 0.6319822536 0.0005354732 0.8055324992 0.8841625380
[11] 0.0726578285 0.6250309648
> gera_particao(x, 2)
 [1] 1 2 1 1 2 1 2 1 2 2 1 2
> gera_particao(x, 3)
 [1] 1 2 2 2 3 1 3 1 3 3 1 2
> gera_particao(x, 4)
 [1] 2 3 2 2 4 1 3 1 4 4 1 3
> gera_particao(x, 12)
 [1]  4  7  5  6 10  3  9  1 11 12  2  8

Alberto Monteiro



More information about the R-help mailing list