【问题标题】:Finding a subset of the `counts` in a histogram in R在 R 的直方图中查找“计数”的子集
【发布时间】:2026-02-20 15:10:01
【问题描述】:

我想知道当 x 轴的值介于 -1 到+1 如下图 10 BLUE 点所示?

set.seed(0)
x = rcauchy(5e4, 0, sqrt(2)/2)
cuts <- quantile(x, c(.025,.975))
cut.data = x[x>=cuts[1] & x<=cuts[2]]
h = hist(cut.data, breaks = 80)
axis(1, at = -9:9, font = 2)

【问题讨论】:

    标签: r plot histogram


    【解决方案1】:

    你可以用

    画出这些点
    with(h, {keep <- (mids>=-1 & mids<=1); points(mids[keep], counts[keep], col="blue", pch=19)})
    

    所以基本上你从h$mids 得到条的中心,检查哪些值在你想要的范围内,然后提取相应的计数

    h$counts[h$mids>=-1 & h$mids<=1]
    

    【讨论】:

      【解决方案2】:

      由 hist 函数返回的直方图对象具有包含直方图单元格边界、单元格中点和每个单元格内的计数的字段。例如:

      # get the counts of the mid points that are between -1 and 1
      binIndices <- (h$mids > -1) & (h$mids < 1)
      midVals <- h$mids[binIndices]
      countVals <- h$counts[binIndices]
      

      【讨论】: