[R] putting stuff into bins...

David Barron mothsailor at googlemail.com
Tue Sep 26 14:13:47 CEST 2006


This would work.  The point is to make a factor from the breakpoints
using cut, then use this to calculate the statistics on the binned
data.

> x <- rnorm(500)
> f <- cut(x,10)
> aggregate(x,list(f),mean)
           Group.1          x
1    (-2.71,-2.09] -2.3668991
2    (-2.09,-1.46] -1.7332011
3   (-1.46,-0.834] -1.1156487
4  (-0.834,-0.208] -0.5117649
5   (-0.208,0.418]  0.1277991
6     (0.418,1.04]  0.7092500
7      (1.04,1.67]  1.2859184
8       (1.67,2.3]  1.9347327
9       (2.3,2.92]  2.5518835
10     (2.92,3.55]  3.2873698


On 26/09/06, Federico Calboli <f.calboli at imperial.ac.uk> wrote:
> Hi All,
>
> I have a vector of data, a vector of bin breakpoints and I want to put my data
> in the bins and then extract fanciful informations like the mean value of each bin.
>
> I know I can write my own function, but I would have thought that R should have
> somewhere a function that took as arguments something like (data, breaks, what
> to do with the data in the bins). I surey could not find it trawling the R-help
> archives though.
>
> If such a function exists I'd be grateful to anyone pointing it out to me.
>
> Cheers,
>
> Fede
>
> --
> Federico C. F. Calboli
> Department of Epidemiology and Public Health
> Imperial College, St Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel  +44 (0)20 7594 1602     Fax (+44) 020 7594 3193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
=================================
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP



More information about the R-help mailing list