lundi 24 décembre 2018

Select random and unique elements from a vector in R

Say a have a simple vector with repeated elements:

a <- c(1,1,1,2,2,3,3,3)

Is there a way to randomly select a unique element from each of the repeated elements? I.e. one random draw pointing which elements to keep would be:

1,4,6 ## here I selected the first 1, the first 2 and the first 3

And another:

1,5,8 ## here I selected the first 1, the second 2  and the third 3

I could do this with a loop for each repeated elements, but I am sure there must be a faster way to do this?

Thanks.




Aucun commentaire:

Enregistrer un commentaire