Search code examples
rbin

get index of the histogram bin in R


Here is my problem:

How can I find the index of the histogram bin in which a number falls?

In Matlab, the solution is easy. HISTC does the job:

[counts,bin] = histc(data,edges)

"bin" being what I am looking for.

But I am working in R, and the hist function of R doesn't propose the functionnality. I think I could manage with some lines of code (using some things as min and <), but as I need to do it for many numbers, I would like to find a more elegant solution.

Since I am not very experienced in R, I hope there could exist a tricky solution, taking to the problem in another way.


Solution

  • The hist function will return the breakpoints between the bins if you do not already have them. You can then use the findInterval function to find which interval/bin each of your points falls into:

    > tmp <- hist(iris$Petal.Width)
    > findInterval(iris$Petal.Width, tmp$breaks)
      [1]  2  2  2  2  2  3  2  2  2  1  2  2  1  1  2  3  3  2  2  2  2  3  2  3  2
     [26]  2  3  2  2  2  2  3  1  2  2  2  2  1  2  2  2  2  2  4  3  2  2  2  2  2
     [51]  7  8  8  7  8  7  9  6  7  7  6  8  6  7  7  7  8  6  8  6 10  7  8  7  7
     [76]  7  7  9  8  6  6  6  7  9  8  9  8  7  7  7  7  7  7  6  7  7  7  7  6  7
    [101] 13 10 11 10 12 11  9 10 10 13 11 10 11 11 13 12 10 12 12  8 12 11 11 10 11
    [126] 10 10 10 11  9 10 11 12  8  7 12 13 10 10 11 13 12 10 12 13 12 10 11 12 10
    > tmp2 <- .Last.value
    > cbind( value=iris$Petal.Width, lower=tmp$breaks[tmp2], upper=tmp$breaks[tmp2+1])
           value lower upper
      [1,]   0.2   0.2   0.4
      [2,]   0.2   0.2   0.4
      [3,]   0.2   0.2   0.4
      [4,]   0.2   0.2   0.4
      [5,]   0.2   0.2   0.4
      [6,]   0.4   0.4   0.6
      [7,]   0.3   0.2   0.4
      [8,]   0.2   0.2   0.4
      [9,]   0.2   0.2   0.4
     [10,]   0.1   0.0   0.2