[R] Best way to coerce numerical data to a predetermined histogram bin?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Dec 6 23:21:47 CET 2012


?cut
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Jonathan Greenberg <jgrn at illinois.edu> wrote:

>Folks:
>
>Say I have a set of histogram breaks:
>
>breaks=c(1:10,15)
>
># With bin ids:
>
>bin_ids=1:(length(breaks)-1)
>
># and some data (note that some of it falls outside the breaks:
>
>data=runif(min=1,max=20,n=100)
>
>***
>
>What is the MOST EFFICIENT way to "classify" data into the histogram
>bins
>(return the bin_ids) and, say, return NA if the value falls outside of
>the
>bins.
>
>By classify, I mean if the data value is greater than one break, and
>less
>than or equal to the next break, it gets assigned that bin's ID (note
>that
>length(breaks) = length(bin_ids)+1)
>
>Also note that, as per this example, the bins are not necessarily equal
>widths.
>
>I can, of course, cycle through each element of data, and then move
>through
>breaks, stopping when it finds the correct bin, but I feel like there
>is
>probably a faster (and more elegant) approach to this.  Thoughts?
>
>--j




More information about the R-help mailing list