[R] "bucketing" observations

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Apr 6 22:00:56 CEST 2009


try this:

dat <- data.frame(vals = rnorm(1000))
breaks <- quantile(dat$vals, seq(0, 1, .1))
dat$bucket <- cut(dat$vals, breaks, labels = FALSE, include.lowest = TRUE)


I hope it helps.

Best,
Dimitris


Dan Dube wrote:
> is there a better way to bucket observations into more-or-less evenly
> sized buckets than this?  it seems like this must be a common operation:
> 
> dt = data.frame(points=rnorm(1000),bucket=NA)
> 
> breaks = quantile(dt$points,seq(0:1,.1))
> for (i in 2:length(breaks)) {
> 	if (i == 2) {
> 		ind = which(dt$points >= breaks[i-1] & dt$points <=
> breaks[i])
> 	} else {
> 		ind = which(dt$points > breaks[i-1] & dt$points <=
> breaks[i])
> 	}
> 	dt$bucket[ind] = i-1
> }
> 
> thanks!
> 
> ______________________________________________
> R-help at r-project.org 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list