【问题标题】:get index of the histogram bin in R获取 R 中直方图 bin 的索引
【发布时间】:2012-01-13 16:21:58
【问题描述】:

这是我的问题:

如何找到数字所在的直方图 bin 的索引?

在 Matlab 中,解决方案很简单。 HISTC 完成这项工作:

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

“bin”是我要找的。​​p>

但我在 R 中工作,而 R 的 hist 函数没有提出功能。我想我可以用几行代码来管理(使用一些东西,如min<),但由于我需要对很多数字这样做,我想找到一个更优雅的解决方案。

由于我对 R 不是很有经验,我希望可以存在一个棘手的解决方案,以另一种方式解决问题。

【问题讨论】:

    标签: r bin


    【解决方案1】:

    hist 函数将返回 bin 之间的断点(如果您还没有它们)。然后,您可以使用findInterval 函数查找每个点属于哪个区间/bin:

    > 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
    

    【讨论】:

      【解决方案2】:

      有几个 R 包(重新)实现 histc 以简化移植代码。例如,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        • 2013-05-31
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多