在总体向量长度>之后,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)
结果看起来正确