Hi R-users, I am trying to divide a vector (say X) into equal frequency bins. If one uses the hist() function, then a histogram is plotted, but with bins of equal widths, and not with bins having the same number of data points. I have then tried the histogram() function as follows: histogram(X, nint=10, breaks=NULL, equal.widths=F) This works as I want. However, I can't extract which points are in which bin, or where the breakpoints are made. Can anybody help? Thanks Daniel -------------------------------------------------------------------- W. Daniel Kissling Institut f?r Zoologie, Abteilung ?kologie Johannes Gutenberg-Universit?t Mainz D-55099 Mainz, Germany E-mail: kissling at uni-mainz.de
Dimitris Rizopoulos
2006-Nov-14 13:16 UTC
[R] dividing vectors into bins with equal widths
you probably looking for something like: x <- rnorm(20) out <- cut(x, quantile(x, seq(0, 1, len = 11)), include.lowest = TRUE) out table(out) I hope it helps. 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: "W. Daniel Kissling" <kissling at uni-mainz.de> To: <r-help at stat.math.ethz.ch> Cc: <kissling at uni-mainz.de> Sent: Tuesday, November 14, 2006 1:52 PM Subject: [R] dividing vectors into bins with equal widths Hi R-users, I am trying to divide a vector (say X) into equal frequency bins. If one uses the hist() function, then a histogram is plotted, but with bins of equal widths, and not with bins having the same number of data points. I have then tried the histogram() function as follows: histogram(X, nint=10, breaks=NULL, equal.widths=F) This works as I want. However, I can't extract which points are in which bin, or where the breakpoints are made. Can anybody help? Thanks Daniel -------------------------------------------------------------------- W. Daniel Kissling Institut f?r Zoologie, Abteilung ?kologie Johannes Gutenberg-Universit?t Mainz D-55099 Mainz, Germany E-mail: kissling at uni-mainz.de ______________________________________________ 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
Hi Daniel, Make sure to include working code and list all needed packages. I couldn't run your code on my machine. If you want equal data points in each class, then you could take every n point from the sorted vector. This example generates the breakpoints to divide a vector in 10 parts.> x <- floor(rnorm(100) * 10) > x[1] -2 9 -12 3 11 7 6 -4 -16 -1 1 -7 -7 7 4 -4 -7 13 1 9 10 1 11 19 -19 -3 12 -6 5 -7 1 -13 -15 [34] 11 -8 -2 -13 10 6 8 15 4 0 -4 12 5 -2 -3 9 -17 -5 9 0 3 7 7 8 -14 -12 -4 -4 -24 -4 4 16 5 [67] 6 -8 -7 -14 -8 -4 -9 18 -2 11 11 -16 -2 2 -1 -8 10 2 21 -9 1 -3 -17 -4 10 -5 -14 7 -13 -6 8 -4 -2 [100] -2> NBreaks <- 10 > sort(x)[NBreaks * (1:(length(x) / NBreaks - 1))][1] -14 -8 -5 -3 -1 2 6 9 11 Cheers, Thierry ---------------------------------------------------------------------------- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -----Oorspronkelijk bericht----- Van: r-help-bounces op stat.math.ethz.ch [mailto:r-help-bounces op stat.math.ethz.ch] Namens W. Daniel Kissling Verzonden: dinsdag 14 november 2006 13:52 Aan: r-help op stat.math.ethz.ch CC: kissling op uni-mainz.de Onderwerp: [R] dividing vectors into bins with equal widths Hi R-users, I am trying to divide a vector (say X) into equal frequency bins. If one uses the hist() function, then a histogram is plotted, but with bins of equal widths, and not with bins having the same number of data points. I have then tried the histogram() function as follows: histogram(X, nint=10, breaks=NULL, equal.widths=F) This works as I want. However, I can't extract which points are in which bin, or where the breakpoints are made. Can anybody help? Thanks Daniel -------------------------------------------------------------------- W. Daniel Kissling Institut f?r Zoologie, Abteilung ?kologie Johannes Gutenberg-Universit?t Mainz D-55099 Mainz, Germany E-mail: kissling op uni-mainz.de ______________________________________________ R-help op 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.