【问题标题】:R: determining frequency of number in histogramR:确定直方图中数字的频率
【发布时间】:2016-09-17 10:02:58
【问题描述】:

假设我有一个直方图,其中 X 数量的观察范围从 0 到 100,中断间隔为 0.00-0.99、1.00-1.99... 等等。我如何关联一个新的观察(假设为 65.5 以供论证) , 与适当中断的观察次数?

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

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

【问题讨论】:

  • 请用数据和代码说明。
  • 仅供参考,调用hist 会创建一个您可以保存和访问的列表。它保存相关数据,包括休息和计数。试试x <- hist(iris$Sepal.Length); str(x); x$breaks; x$counts。对于新的观察,您可以执行with(x, counts[which.max(breaks > 6.3)])

标签: r histogram frequency


【解决方案1】:

这是你想做的代码:

# 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

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 2018-06-08
  • 2020-01-27
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 2011-12-06
相关资源
最近更新 更多