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
Federico Calboli <f.calboli at imperial.ac.uk> writes:> 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.cut, split+lapply, aggregate, by -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
I don't know about such a function, but tapply(data,cut(data,breaks),what to do) should give you what you need. HIH Ciao, Stefano On Tue, Sep 26, 2006 at 12:44:35PM +0100, Federico Calboli wrote: <Federico>Hi All, <Federico> <Federico>I have a vector of data, a vector of bin breakpoints and I want to put my data <Federico>in the bins and then extract fanciful informations like the mean value of each bin. <Federico> <Federico>I know I can write my own function, but I would have thought that R should have <Federico>somewhere a function that took as arguments something like (data, breaks, what <Federico>to do with the data in the bins). I surey could not find it trawling the R-help <Federico>archives though. <Federico> <Federico>If such a function exists I'd be grateful to anyone pointing it out to me. <Federico> <Federico>Cheers, <Federico> <Federico>Fede <Federico> <Federico>-- <Federico>Federico C. F. Calboli <Federico>Department of Epidemiology and Public Health <Federico>Imperial College, St Mary's Campus <Federico>Norfolk Place, London W2 1PG <Federico> <Federico>Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 <Federico> <Federico>f.calboli [.a.t] imperial.ac.uk <Federico>f.calboli [.a.t] gmail.com <Federico> <Federico>______________________________________________ <Federico>R-help a stat.math.ethz.ch mailing list <Federico>https://stat.ethz.ch/mailman/listinfo/r-help <Federico>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <Federico>and provide commented, minimal, self-contained, reproducible code.
Federico Calboli schrieb:> 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 > >The following should be of help: "bd384" <- c(2.968, 2.097, 1.611, 3.038, 7.921, 5.476, 9.858, 1.397, 0.155, 1.301, 9.054, 1.958, 4.058, 3.918, 2.019, 3.689, 3.081, 4.229, 4.669, 2.274, 1.971, 10.379, 3.391, 2.093, 6.053, 4.196, 2.788, 4.511, 7.3, 5.856, 0.86, 2.093, 0.703, 1.182, 4.114, 2.075, 2.834, 3.698, 6.48, 2.36, 5.249, 5.1, 4.131, 0.02, 1.071, 4.455, 3.676, 2.666, 5.457, 1.046, 1.908, 3.064, 5.392, 8.393, 0.916, 9.665, 5.564, 3.599, 2.723, 2.87, 1.582, 5.453, 4.091, 3.716, 6.156, 2.039) cut(bd384,0:11) split(bd384,cut(bd384,0:11)) sapply(split(bd384,cut(bd384,0:11)),mean) D.Trenkler -- Dietrich Trenkler c/o Universitaet Osnabrueck Rolandstr. 8; D-49069 Osnabrueck, Germany email: Dietrich.Trenkler at Uni-Osnabrueck.de
probably a combination of cut() and tapply() could be of help in this case, e.g., x <- rnorm(100) tapply(x, cut(x, -4:4), mean) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Federico Calboli" <f.calboli at imperial.ac.uk> To: "r-help" <r-help at stat.math.ethz.ch> Sent: Tuesday, September 26, 2006 1:44 PM Subject: [R] putting stuff into bins...> 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
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