I've been struggling myself to get a random sampling of my data in a for loop with R, but I need help. It is my first time writing a loop and doing random sampling... Here there is a reproducible script of what I so far have. I would like to run 5 times the loop to get different samples containing 3 random and unique observations each time.
So each iteration will give me one observation for 'virginica', one for 'setosa' and one for 'versicolor', different every time.
My problem is that the two functions that I am using (sample and duplicated) are acting one after the other and I need them to act together cause I want to have always the same final number of samples (N=3).
And a final question: how can I save the results?
Any help? Thanks in advance!
for(i in 1:5){ # Number of repetitions of the loop
rnd.iris <- iris[sample(1:nrow(iris),3,replace=FALSE),] # Take a random sample of size 3 from a dataset "iris"; sampling without replacement
rnd.iris <- rnd.iris[!duplicated(rnd.iris$Species),] # Eliminate replicates (but it does it after the randomization -> wrong for my purpose)
print(rnd.iris)
}
Aucun commentaire:
Enregistrer un commentaire