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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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



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.


N=200


0


i= 3:N


i


0


1


0


i


V



if the random number is greater than the numerical value, I simply consider the next i.


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.




1 Answer
1



Your problem is that most functions in R do not modify objects they are applied to. Instead, they return a modified version of the original object which you have to assign, either to a new object, or to the original one (update it).


R



So instead of just running replace(Time, i, 1) (which does not update the Time vector), you need to assign the modified version back to Time like this:


replace(Time, i, 1)


Time


Time


Time <- replace(Time, i, 1)



or, better yet, use square bracket notation to update directly:


Time[i] <- 1



Moreover, you need to update vector Time, Time <- replace(Time, i, 1), before you update your index i, i = i + 2. If you want to apply your loop only to i > 3, then you need to set i = 4. Finally, in case runif(1) < 0.66, if you want to skip the next two entries in Time, you should update i as i <- i + 3.


Time


Time <- replace(Time, i, 1)


i


i = i + 2


i > 3


i = 4


runif(1) < 0.66


Time


i


i <- i + 3



To summarise, you should modify your loop as follows:


N = 200
Time <- rep(0, N)
Time[3] <- 1
i <- 4

while (i <= N)
p <- runif(1)
if(p < 0.66)
Time[i] <- 1
i <- i + 3
else
i <- i+1






Good answer! For a fully complete answer, you should strengthen your recommendation to use square bracket notation over functions like replace or subset: stackoverflow.com/questions/9860090/why-is-better-than-subset
– divibisan
Aug 6 at 16:49


replace


subset





@divibisan agreed, thank you!
– stas g
Aug 6 at 17:01





@divibisan replace is [<-, so I'm not sure that criticism applies. I guess a real improvement for the OP would come from using vectorized code instead of a loop...
– Frank
Aug 6 at 17:07


replace


[<-





@Frank: It's hard to say the best way to vectorize this program since it's not really clear (at least to me) what OP wants to do and so it's hard to say how to do it better. If you have an idea, though, you should write your own answer!
– divibisan
Aug 6 at 17:17





@Frank Good point on replace, I didn't look at the source before. I still think it shouldn't be used for purposes of clarity, but you're right that there shouldn't be any risks to it.
– divibisan
Aug 6 at 17:20


replace






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS