Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取R中直方图的索引_R_Bin - Fatal编程技术网

获取R中直方图的索引

获取R中直方图的索引,r,bin,R,Bin,我的问题是: 如何找到某个数字所在的柱状图箱的索引 在Matlab中,解决方案很简单。HISTC负责以下工作: [counts,bin] = histc(data,edges) “宾”是我要找的 但是我在R工作,R的hist函数并没有提出函数性。我想我可以使用一些代码行(使用一些东西,如min和,hist函数将返回桶之间的断点,如果您还没有断点的话。然后,您可以使用findInterval函数来查找每个点属于哪个间隔/桶: > tmp <- hist(iris$Petal.Widt

我的问题是:

如何找到某个数字所在的柱状图箱的索引

在Matlab中,解决方案很简单。HISTC负责以下工作:

[counts,bin] = histc(data,edges)
“宾”是我要找的


但是我在R工作,R的
hist
函数并没有提出函数性。我想我可以使用一些代码行(使用一些东西,如
min
hist
函数将返回桶之间的断点,如果您还没有断点的话。然后,您可以使用
findInterval
函数来查找每个点属于哪个间隔/桶:

> 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
>tmp 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 cbind(值=iris$Petal.Width,下=tmp$breaks[tmp2],上=tmp$breaks[tmp2+1])
值下限上限
[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

有两个R包(重新)实现了
histc
,以简化代码的移植

  • pracma