在总体向量长度>之后,R返回非随机样本中的Sample(). 13.为什么? [英] Sample() in R returning non-random sample after population vector length > 13. Why?

查看:90
本文介绍了在总体向量长度>之后,R返回非随机样本中的Sample(). 13.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code will return a perfectly sound sample:

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

将元素数增加1到14将导致以下结果:

Increasing the number for elements by 1 to 14 will result into this:

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

这显然是不正确的.零发生的次数比预期的多.有这个原因吗?

That's clearly not correct. Zero occurs more often that it should. Is there a reason for this?

推荐答案

问题出在hist,而不是sample.

您可以检查是否这样做:

You can check that doing:

> 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.

If right = TRUE (default), the histogram cells are intervals of the form (a, b], i.e., they include their right-hand endpoint, but not their left one, with the exception of the first cell when include.lowest is TRUE.

对于right = FALSE,间隔的格式为[a,b),并且 include.lowest表示包括最高".

For right = FALSE, the intervals are of the form [a, b), and include.lowest means ‘include highest’.

如果您尝试

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

结果看起来正确

这篇关于在总体向量长度&gt;之后,R返回非随机样本中的Sample(). 13.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆