[R] Histogram Bin

Sean Anderson sc2ander at xcskier.com
Fri May 14 16:46:02 CEST 2010


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



More information about the R-help mailing list