[R] "bucketing" observations
Dan Dube
ddube at advisen.com
Mon Apr 6 21:45:27 CEST 2009
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!
More information about the R-help
mailing list