r - multiple next statements in function -


I am trying to run a function more complex but equal to:

  A & Lt; & Lt; - 5 B & lt; & Lt; - Table 5 & lt; - Data. Frame (A, B) Backup & lt; - Table test & lt; - Function (A, B) {While (A & gt; 0) {R & Lt; - runif (1,0,1) if ((& gt; = 0) & amp; (R & T; = 0.5)) {A & lt; - A + 1b & lt; - B - 1} and if ((R> 0.5) & amp; (R & lt; = 1)) {A & lt; - A - 1b <+ B + 1 tab & lt; - cbind (A, B) table & lt; & Lt; - rbind (table, tab) if (now (table) == 10) {break} else if ((A & gt; 0) & amp; (nrow (table)! = 10)) {next} else {if ( (A == 0) & amp; (Now (table! = 10)) # # Error given here? A <- 5 B <- 5 table & lt; - Backup next}}} < / Code> 

So what do I want is that it stops working when the number of rows in the table (= number of number of function visits) is equal to a certain value, in this case 10 Can not take any value under 0. If A is the number of rows in the table Or before 10, 10 complete process must be started with the same input voltage already

However, this function does not work. I think this is because I have many other statements I use it, right?

Thank you!

I think That you are on the right track ... just a few suggestions

I think that with this you should get some cleaner Are trying to meet it. Global Operator '& lt; & Lt; - The second Karl suggestion to avoid 'when possible and passes the object as argument or parameter through the function, I suggest second Justin to avoid the brake command in favor of keeping return () Call it cleverly to complete it, I put your 10 (stop criteria) in the loop directly at that time. I have included this as a parameter n so that you can experiment with various stop criteria. Hope this helps :)

  test & lt; - Function (A, B, N) {A <- A B0 < - B tab & lt; - Data. Frame (A, B) while (A and gt; 0 & amp; nrow (tab) & lt; n) {R & lt; - runif (1,0,1) if (R & lt; = 0.5) {A & lt; - A + 1b & lt; - B - 1} and (A and LT; - A - 1B & LT; - B + 1} tab 1 & lt; - cbind (A, B) tab & lt; - RBID (Tab, Tab 1) If (Nero (tab) & lt; n & amp;; A == 0 (A & lt; -5b & lt; -5 tab & lt; - cbind (A0, B0) Name (tab) & lt; - c ('A', 'B') Print (Paste ('found A = / 0>  

test function ...

  ## Run the operation A & lt; - 5b & lt; 5 testdf & lt; - test (a, b, 10) & gt; ; TestDF A B1 5 5 2 6 4 3 5 5 4 6 4 5 7 3 6 8 2 7 9 8 8 9 9 1 10 10 0  

Comments