[英]R: trouble with specifying probability for sample function
> sample(c(2), 10, replace = TRUE, prob = 1)
Error in sample.int(x, size, replace, prob) :
incorrect number of probabilities
> sample(c(1), 10, replace = TRUE, prob = 1)
[1] 1 1 1 1 1 1 1 1 1 1
In the first example, I would like to sample the vector 2
ten times, with replacement, each with probability = 1. I would expect the output to be 2 2 2 2 2 2 2 2 2 2
在第一个示例中,我想对向量2
十次采样,并进行替换,每次概率为1。我希望输出为2 2 2 2 2 2 2 2 2 2
However, it seems to work with a vector of 1
? 但是,似乎可以使用1
?向量。
Try removing the prob = 1 and what do you get? 尝试删除概率= 1,您会得到什么?
> set.seed(123)
> sample(c(2), 10, replace = TRUE)
# [1] 1 2 1 2 2 1 2 2 2 1
help(sample)
Usage 用法
sample(x, size, replace = FALSE, prob = NULL)
If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. 如果x的长度为1,则为数字(在is.numeric的意义上),并且x> = 1,则通过样本从1:x进行采样。 Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x). 请注意,当x在诸如sample(x)之类的调用中具有不同长度时,此便利功能可能会导致不良行为。 See the examples. 请参阅示例。
So, it's sampling from 1:2
not 2
. 因此,它是从1:2
而不是2
采样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.