[R] binning data

Marc Schwartz marc_schwartz at comcast.net
Mon Jan 14 17:20:27 CET 2008

Robert Terwilliger wrote:
> I would like to 'bin' a vector of of values (>100000 values) in much
> the same way as 'hist' does for plotting, but I just want a result
> with the bins and the frequencies.
> Is there a 'quick and dirty' (built-in) way to do this, or do I need
> to write some code?
> Many thanks,
> Robert Terwilliger

If you want the SAME bins as used by hist(), you can use:

   hist(Vec, breaks = ..., plot = FALSE)$counts

Another alternative is to use cut() to bin a vector and return a factor, 
upon which you can then use table() to get the counts. For example:

   table(cut(Vec, breaks = ...))

See ?hist and ?cut for more information.


Marc Schwartz

More information about the R-help mailing list