Hello, Is there a function that returns the number of the "bin" (or quantile, or percentile etc. etc.) that a value of a variable may belong to? Tor example: breaks<-hist(variable, 18, plot=FALSE) If the following breaks are 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 the boundaries of successive bins of a histogram, then value "6" belongs to the 2nd bin. Best regards, Costas
Histogram Bin
4 messages · Research, Robert A LaBudde, Sean Anderson +1 more
x<- rnorm(200)
> hist(x, 18) > str(hist(x, 18)) List of 7 $ breaks : num [1:15] -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 ... $ counts : int [1:14] 3 1 8 12 34 35 40 30 18 11 ... $ intensities: num [1:14] 0.03 0.01 0.08 0.12 0.34 ... $ density : num [1:14] 0.03 0.01 0.08 0.12 0.34 ... $ mids : num [1:14] -2.75 -2.25 -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75 ... $ xname : chr "x" $ equidist : logi TRUE - attr(*, "class")= chr "histogram" > hist(x, 18, plot=FALSE)$breaks [1] -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
At 09:55 AM 5/14/2010, Research wrote:
Hello, Is there a function that returns the number of the "bin" (or quantile, or percentile etc. etc.) that a value of a variable may belong to? Tor example: breaks<-hist(variable, 18, plot=FALSE) If the following breaks are 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 the boundaries of successive bins of a histogram, then value "6" belongs to the 2nd bin. Best regards, Costas
______________________________________________ R-help at r-project.org 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.
================================================================ Robert A. LaBudde, PhD, PAS, Dpl. ACAFS e-mail: ral at lcfltd.com Least Cost Formulations, Ltd. URL: http://lcfltd.com/ 824 Timberlake Drive Tel: 757-467-0954 Virginia Beach, VA 23464-3239 Fax: 757-467-2947 "Vere scire est per causas scire"
On Fri, May 14, 2010 at 10:55 AM, Research <risk2009 at ath.forthnet.gr> wrote:
Is there a function that returns the number of the "bin" (or quantile, or percentile etc. etc.) that a value of a variable may belong to?
Something like this should work:
dat <- round(runif(20, 0, 100))
hist.dat <- hist(dat, plot = FALSE)
get.break.num <- function(x, breaks) {
min((1:length(breaks))[x < breaks]) - 1
}
# Then to use it:
get.break.num(6, hist.dat$breaks)
[1] 1
get.break.num(42, hist.dat$breaks)
[1] 3 Sean
On May 14, 2010, at 9:55 AM, Research wrote:
Hello, Is there a function that returns the number of the "bin" (or quantile, or percentile etc. etc.) that a value of a variable may belong to? Tor example: breaks<-hist(variable, 18, plot=FALSE) If the following breaks are 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85
> findInterval(6, c(-Inf, brks) ) [1] 2
the boundaries of successive bins of a histogram, then value "6" belongs to the 2nd bin.
The first actually, but if you were considering the left hand boundary as -Inf, then you need to add it to the vector to get the correct answer from findInterval.
Best regards, Costas
______________________________________________ R-help at r-project.org 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 Winsemius, MD West Hartford, CT