R中的样本()在群体向量长度之后返回非随机样本> 13.为什么?

时间:2015-01-18 01:18:57

标签: r sample random-sample

以下代码将返回完美的声音样本:

b <- sample(c(0,1,2,3,4,5,6,7,8,9,10,11,12), 100000, replace=TRUE)
hist(b)

enter image description here

将元素数量增加1到14将导致:

b <- sample(c(0,1,2,3,4,5,6,7,8,9,10,11,12,13), 100000, replace=TRUE)
hist(b)

enter image description here

这显然不正确。零发生的次数应该更多。有这个原因吗?

1 个答案:

答案 0 :(得分:0)

问题在于hist,而不在sample

你可以检查一下:

> table(sample(0:15, 10000, replace=T))

  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 
634 642 664 654 628 598 633 642 647 625 587 577 618 645 615 591 

来自hist帮助:

  

如果right = TRUE(默认值),则直方图单元格为   形式(a,b),即它们包括它们的右手端点,但不是   他们的左边一个,除了第一个单元格的时候   include.lowest为TRUE。

     

对于right = FALSE,间隔的形式为[a,b),和   include.lowest意味着'包括最高'。

如果您尝试

hist(sample(0:15, 10000, replace=T), br=-1:15)

结果看起来是正确的