lundi 6 août 2018

Getting R to skip certain index values with the help of if statements, within while loops.

I apologize if this question has been asked before. I have been through multiple R-while-if questions and I didn't find a similar one. Please guide me to the answer if it has been asked.

So I have a vector V of N=200 entries of number 0. Every entry signifies a "bin". I skip the first 3 bins, for separate reasons. Now for i= 3:N number of bins, I use a while loop.

  • For each ith number of bin, I generate a random number between 0 and 1.
  • If the random number is less than a certain numerical value (here it's 0.66) then, I simply replace the 0 at index i in Vector V with 1 and at the same time, I want while loop to skip next two values.
  • if the random number is greater than the numerical value, I simply consider the next i.

    Here's the code I am running:

    N = 200
    Time <- rep(0, N)
    Time <- replace(Time, 3, 1)
    i = 5
    while (i <= N){
    p <- runif(1)
     if(p < 0.66){
       i= i+ 2; replace(Time, i, 1)
      } else {
        i <- i+1
       }
     }
    
    

I am not very good at R, and this is the combination I have been trying to use, to get R to do what I want, but it's clearly not working. I might be missing something obvious. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire