get index of the histogram bin in R

前端 未结 2 829
星月不相逢
星月不相逢 2020-12-21 13:18

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:



        
相关标签:
2条回答
  • 2020-12-21 14:03

    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
    
    0 讨论(0)
  • 2020-12-21 14:08

    There are a couple of R packages that (re)implement histc to ease porting code. For example,

    • pracma http://cran.r-project.org/web/packages/pracma/
    0 讨论(0)
提交回复
热议问题