[R] How to do a "go to " in a loop in R

Gabor Grothendieck ggrothendieck at myway.com
Wed Jul 21 22:17:09 CEST 2004

Lisa Wang <lisawang <at> uhnres.utoronto.ca> writes:

: Hi there,
: I'm writing a function which involves a loop. What to write in the "?"
: place would allow it skips the "for loop" and goes to  "a[i]<-0".
:  a<-b[s>3,]
:  if (nrow(a)==0) ?????????????
: 	for (i in 1:nrow(a)){
:   		a[i]<-1
: 	}
:  a[i]<-0

There are a number of errors in the above but I assume your question
is how to set up a loop over an index of the rows of  a  so that the
loop executes zero times if a has no rows.  Try this:

  for(i in seq(len=nrow(a))) ...

Also, depending on what you want to do, you might consider whether you 
need a loop in the first place.

More information about the R-help mailing list