R语言 cannot take a sample larger than the population when 'replace = FALSE'

> x <- seq(1,10);x
 [1]  1  2  3  4  5  6  7  8  9 10
> # 利用sample函数对x进行无放回抽样
> a <- sample(x,8,replace=FALSE);a
[1] 10  6  5  4  1  8  2  7
> # 利用sample函数对x进行有放回抽样
> b <- sample(x,8,replace=TRUE);b
[1]  7  6 10  6  4  8  4  4
> # 当size大于x的长度
> (c <- sample(x,15,replace = F))
Error in sample.int(length(x), size, replace, prob) : 
  cannot take a sample larger than the population when 'replace = FALSE'

解决方法:

> (c <- sample(x,15,replace = T))
 [1]  5  8  4  8  3  4 10  5  2  8  4  3  7  9  3

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章