R:确定直方图中的数字频率

时间:2016-05-20 08:23:03

标签: r histogram frequency

假设我有一个直方图,其中X的观察范围从0到100,间隔为0.00-0.99,1.00-1.99 ......等。我如何关联一个新的观察结果(让我们说65.5)为了论证,有适当休息的观察数量?

使用频率表而不是直方图会使这更容易吗?

如果没有明确表述,请告诉我,我会尝试澄清。

1 个答案:

答案 0 :(得分:3)

以下是执行您想要的代码:

# Setup
data = runif(10000)
h = hist(data, breaks = seq(0,1,length.out = 101))

# New observation
newdata = runif(1)

# Get the bin for the new value
position = findInterval(newdata, h$breaks)

# Extract the counts
counts = h$counts[position]

# Test the counts are correct (for this experiment)
countstest = sum(floor(data*100) == floor(newdata*100))

show(c(counts, countstest))

## [1] 93 93