[R] Binning question (binning rows of a data.frame
David Duffy
David.Duffy at qimr.edu.au
Mon Mar 20 22:06:52 CET 2006
Dan Bolser <dmb at mrc-dunn.cam.ac.uk> wrote:
>
> Gabor Grothendieck wrote:
> > Do you mean you want g to be in the original order of x?
>
> No. What I mean is that I want to order x by any particular variable in
> my data.frame, then group over x such that each group has roughly the
> same sum.
>
> I get the feeling I have missed a very simple trick.
Maybe something like:
x <- data.frame(i=sample(1:10),j=1:10)
x2 <- x[order(x$i),]
x2$k <- cumsum(x2$j)
#
# 4 bins (;))
#
x2$bins <- cut(x2$k,quantile(x2$k), labels=F)
x2
David Duffy.
More information about the R-help
mailing list