This is a longstanding problem when programing with seq. In the common case that one wants 1,...,n such that if n is zero a zero length answer is wanted, one can write seq(length = n) instead of 1:n. However, the general case is still a problem, as you point out.
On 8/23/06, Dominick Samperi <[EMAIL PROTECTED]> wrote: > I should point out that this recommendation was not designed just > to accommodate C/C++, it was motivated by problems > with R code of the form: > for(m in seq(beg,end)) > val <- x[m] > > Take beg=1, end=0, for example. This results in > val <- x[0] > > and R issues no error about the subscript being out > of bounds. Later when val is used you get not very > informative error messages about variables of > length zero (because val has length zero after the > above assignment). > > ds > > > Thomas Lumley wrote: > > On Wed, 23 Aug 2006, Dominick Samperi wrote: > > > >> In for loops of the form: > >> for(m in seq(beg,end)) > >> ... > >> > >> when beg > end, the default behavior is > >> to decrement, and the action described > >> by ... is executed at least once. > >> > >> On the other hand, if you view this > >> construction as a translation of the C code: > >> for(m = beg; m < end; m++) > >> ... > >> > >> then the semantics of C/C++ is not > >> respected, because the code in ... is > >> not executed when beg > end in > >> the case of C/C++. > > > > There is another important way in which the C loop is different from > > the R loop. If you modify m or end inside the loop the number of > > iterations of the C loop changes but the number of iterations of the R > > loop doesn't. R's for() loop just isn't the same as C's. > > > > -thomas > > > > Thomas Lumley Assoc. Professor, Biostatistics > > [EMAIL PROTECTED] University of Washington, Seattle > > > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel