mardi 5 janvier 2016

Create a sample with at least one of each element in R

Is there a way to create a sample with size n of a vector with x elements that will have at least one occurrence of each x element given that n >= length(x)?

Example:

> 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

I want to have each element ocurring at least one time, how is it possible? Should I use another function?




Aucun commentaire:

Enregistrer un commentaire