r - Get out of infinite while loop -


what best way have while loop recognize when stuck in infinite loop in r?

here's situation:

diff_val = inf last_val = 0  while(diff_val > 0.1){      ### calculate val data subset greater previous iteration's val     val = foo(subset(data, col1 > last_val))      diff_val = abs(val - last_val) ### how did change val?     last_val = val ### set last_val next iteration } 

the goal have val progressively closer , closer stable value, , when val within 0.1 of val last iteration, deemed sufficiently stable , released while loop. problem data sets, val gets stuck alternating , forth between 2 values. example, iterating , forth between 27.0 , 27.7. thus, never stabilizes. how can break while loop if occurs?

i know of break not know how tell loop when use it. imagine holding onto value 2 iterations before work, not know of way keep values 2 iterations ago...

while(diff_val > 0.1){      val = foo(subset(data, col1 > last_val))      diff_val = abs(val - last_val)     last_val = val      if(val == val_2_iterations_ago) break } 

how can create val_2_iterations_ago?

apologies non-reproducible code. real foo() , data needed replicate situation not mine share... aren't key figuring out issue control flow, though.

i don't know if keeping track of previous 2 iterations suffice, isn't trouble add logic this.

the logic @ each iteration, second last value becomes last value, last value becomes current value, , current value derived foo(). consider code:

while (diff_val > 0.1) {     val <- foo(subset(data, col1 > last_val))      if (val == val_2_iterations_ago) break      diff_val = abs(val - last_val)     val_2_iterations_ago <- last_val     last_val <- val } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -