samedi 1 mai 2021

R programming: How to set while loop condition based if all required values in vector have been copied from sample?

I new to R and I'm trying to see how many iterations are needed to fill a vector with numbers 1 to 55 (no duplicates) from a random sample using runif.

At the moment, the vector has a lots of duplicates in it and my number of iterations being returned is the size of the vector. So, i'm not sure if my logic is correct. The aim of the if statement is to check if the value from the sample exists in the vector, and if it does, choose the next one. But i'm not sure if it's correct, since the next number could already exist in the vector. Any help would be much appreciated

numbers=as.integer(runif(800, min=1, max=55)) ## my sample from runif
i=sample(numbers, 1) 

## setting up my vector to store 55 unique values (1 to 55)
p=rep(0,55)

## my counters
j=0
n=1

## my while loop
while (p[n] %in% 0){
## if the sample value already exists in the vector, choose the next value from the sample
if (numbers[n] %in% p) {
p[n]=numbers[n+1] 
}
else {
p[n] = numbers[n] 
}
    n = n + 1
    j = j + 1
}

Aucun commentaire:

Enregistrer un commentaire