jeudi 26 mars 2020

Breaking loops with a conditional that looks at past results generated by loop? Setting conditional on an earlier value in a vector

I am trying to break a loop by having a conditional that checks whether the last value of each of three vectors satisfies a particular condition that that may have taken place at that iteration OR past iterations of the loop.

Below is a very simple version of what I am trying to do.

Consider the following loop:

period = c(1:100)

x1 = 6
x2 = 8
x3 = 10
x = c()

for(t in 1:length(period)){x[t] = sample(c(x1,x2,x3), size = 1, replace = TRUE, prob = c(0.6,0.35,0.05))}

I am just wondering how I can make it such that this loop stops once each value has been sampled at least once. That is, the loop stops when each of x1,x2,x3 have been sampled.

Thanks

Aucun commentaire:

Enregistrer un commentaire