是否可以使用n
元素创建一个大小为x
的样本的方法,该元素至少会出现x
个元素n >= length(x)
?
示例:
> n = 10 # size of the sample
> vector <- c("a", "b", "c", "d") #
> sample(vector, size = n, replace = TRUE) #
[1] "b" "c" "b" "d" "b" "d" "c" "d" "c" "d" # "a", by chance, don't get selected
我希望每个元素至少出现一次,怎么可能?我应该使用其他功能吗?
输出示例:
> "a" "b" "b" "b" "c" "c" "c" "c" "c" "d"
> "c" "c" "a" "c" "d" "c" "c" "c" "b" "d"
> "a" "b" "c" "d" "a" "a" "a" "a" "a" "a"