# Why doesn't this nested loop work?

5 messages
Open this post in threaded view
|

## Why doesn't this nested loop work?

 Hi there,   I can get the for-loop to work, I can get the while loop to work.  But I can't get a for loop to work nested within the while loop - why?     Please help, Teresa ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html nested_loop_question.txt (1K) Download Attachment
Open this post in threaded view
|

## Re: Why doesn't this nested loop work?

Open this post in threaded view
|

## Re: Why doesn't this nested loop work?

 In reply to this post by Teresa Nelson Teresa Nelson > Hi there, > > > > I can get the for-loop to work, I can get the while loop to work.  But I > can't get a for loop to work nested within the while loop - why? > > > > Please help, > > Teresa It actually does work, but I think the problem is with your matrix indexing.  See the sample below.   You could also check out ?seq, ?rep, and ?apply for solving this problem with fewer lines of code.  n.max <- 300  NUM <- 25    n.sim <- 10  j <- (n.max/NUM)*n.sim    results <- matrix(0, nrow=j, ncol=2)    while(NUM <= n.max){    for(i in 1:n.sim){    k <- (NUM/25)*i    results[((NUM / 25) - 1) * n.sim + i, 1] <- k  results[((NUM / 25) - 1) * n.sim + i, 2] <- NUM  cat("Iteration", i, ": NUM=", NUM, "k=", k, "\n")    }    NUM <- NUM + 25  } This email message, including any attachments, is for the so...{{dropped}} ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html