[R] Trouble With for() Loops

Duncan Murdoch murdoch at stats.uwo.ca
Wed Mar 3 12:53:07 CET 2010

On 03/03/2010 2:27 AM, Milo Phillips-Brown wrote:
> Hello,
> I'm trying to learn R (for fun!), and I've tried to find any previous 
> discussion of this problem (or something like it) and haven't found one.
> When I run the following command, R seems to simply stop. I get no error 
> message, but I also have no ">" or "+" to type after (although I can 
> type). The only solution seems to be restarting the program. Here's the 
> code:
> for(i in 1:150)
> for(j in 1:150)
> (average <-c(
> (if(NO[i]==1)
> {i*0}
> else
> {if(HA[i]==HA[j])
> {while(G[j]==1)
> {mean(PT[j:(i-1)])}}})))

That's very strange formatting, mixing () and {} and not doing any 
indenting, so I don't know what you're trying to do.  But the problem 
appears to be your while loop:  you never change the condition G[j] == 
1, so that loop will continue indefinitely.

Duncan Murdoch

> Also, if what I'm trying to do makes any sense and someone sees an 
> easier way of doing it, that would be great.
> Thanks!
> Milo
> Undergraduate -- Reed College
> Portland, OR
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list