[R] Partition into quantiles
Is there any function that divides a sample into N quantiles? For example, for N = 2, this would be the solution: x - rnorm(100) m - median(x) q - ifelse(x = median, 1, 2) Alberto Monteiro __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Partition into quantiles
Alberto Monteiro [EMAIL PROTECTED] writes: Is there any function that divides a sample into N quantiles? For example, for N = 2, this would be the solution: x - rnorm(100) m - median(x) q - ifelse(x = median, 1, 2) Have a look at N - 2 table(cut(x,quantile(x,seq(0,1,1/N)), include.lowest=TRUE)) [-2.78,0.205] (0.205,2.22] 5050 table(cut(x,c(-Inf,quantile(x,(1:(N-1))/N),Inf))) (-Inf,0.205] (0.205, Inf] 50 50 -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Partition into quantiles
cut.quantile - function(x, N, use.ppoints=TRUE) { qq - if (use.ppoints) c(0,ppoints(N-1),1) else seq(0, 1, 1/N) breaks - quantile(x, qq) breaks[1] - breaks[1] - 1 breaks[N+1] - breaks[N+1]+1 cut(x, breaks) } tmpT - cut.quantile(rnorm(100), 10, TRUE) table(tmpT) tmpF - cut.quantile(rnorm(100), 10, FALSE) table(tmpF) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Partition into quantiles
You might also want to look at the function quantcut in the gtools package (part of the gregmisc bundle). On 05/10/06, Alberto Monteiro [EMAIL PROTECTED] wrote: Is there any function that divides a sample into N quantiles? For example, for N = 2, this would be the solution: x - rnorm(100) m - median(x) q - ifelse(x = median, 1, 2) Alberto Monteiro __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Partition into quantiles
David Barron wrote: You might also want to look at the function quantcut in the gtools package (part of the gregmisc bundle). Also, cut2 in Hmisc will do this and will label the intervals compactly. Frank On 05/10/06, Alberto Monteiro [EMAIL PROTECTED] wrote: Is there any function that divides a sample into N quantiles? For example, for N = 2, this would be the solution: x - rnorm(100) m - median(x) q - ifelse(x = median, 1, 2) Alberto Monteiro __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Partition into quantiles
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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.