Hi, This may belong more to r-develop, but general discussion may be useful (for the how many-th time ?) seq(2,5,-2) seq(5,2,2) both result in Error in seq.default(2, 5, -2) : wrong sign in 'by' argument But often, if not always, mathematicians and programmers want a behaviour e.g. in for loops, where this statement results in an empty statement, that is for (ii in seq(2,5,-2)) print(ii) were equivalent to for (ii in NULL) print(ii). The relevant part in seq.default is now if (n < 0) stop("wrong sign in 'by' argument") but could be changed by option to return(NULL) I think there should be an option to seq requiring this behaviour, or a specific function, may be even a special operator, e.g. %;%: 3;5 resulting in NULL. What do you think? Christian -- Dr. Christian W. Hoffmann, Swiss Federal Research Institute WSL Mathematics + Statistical Computing Zuercherstrasse 111 CH-8903 Birmensdorf, Switzerland Tel +41-44-7392-277 (office) -111(exchange) Fax +41-44-7392-215 (fax) christian.hoffmann at wsl.ch http://www.wsl.ch/staff/christian.hoffmann International Conference 5.-7.6.2006 Ekaterinburg Russia "Climate changes and their impact on boreal and temperate forests" http://ecoinf.uran.ru/conference/
On 3/27/2006 4:41 AM, Christian Hoffmann wrote:> Hi, > > This may belong more to r-develop, but general discussion may be useful > (for the how many-th time ?) > > seq(2,5,-2) > seq(5,2,2) > > both result in > > Error in seq.default(2, 5, -2) : wrong sign in 'by' argument > > But often, if not always, mathematicians and programmers want a > behaviour e.g. in for loops, where this statement results in an empty > statement, that is > > for (ii in seq(2,5,-2)) print(ii) > > were equivalent to > > for (ii in NULL) print(ii). > > The relevant part in seq.default is now > > if (n < 0) > stop("wrong sign in 'by' argument") > > but could be changed by option to > > return(NULL) > > I think there should be an option to seq requiring this behaviour, or a > specific function, may be even a special operator, e.g. %;%: > > 3;5 resulting in NULL. > > What do you think?If you want optional behaviour, the easiest way is to write your own wrapper function. E.g. emptyseq <- function(from, to, by) { if ((to-from)*by < 0) return(NULL) else return(seq(from, to, by)) } I don't think this is a desirable default, though. We already have a special way to handle the most common case, i.e. seq(1, length(x), 1) should be written as seq(along=x)) to handle the length(x) == 0 case the way you're requesting. But I'm not so sure that seq(2,5,-2) should really be NULL; it looks much more like an error to me. You say mathematicians and programmers want this behaviour, but I really can't think of any examples other than the one above. As a general principle, I think it's better to throw an error on ambiguous or apparently erroneous code rather than always returning an answer. If the code can be made unambiguous, it should be. (R doesn't always follow this principle; for example, recycling of vectors of lengths bigger than 1 is probably an error at least as often as it's intended.) Duncan Murdoch
You should be able to do it yourself; e.g., my.seq <- function(...) if((to - from) * by < 0) NULL else seq(...) and use that instead when you want that behavior. Andy From: Christian Hoffmann> > Hi, > > This may belong more to r-develop, but general discussion may > be useful > (for the how many-th time ?) > > seq(2,5,-2) > seq(5,2,2) > > both result in > > Error in seq.default(2, 5, -2) : wrong sign in 'by' argument > > But often, if not always, mathematicians and programmers want a > behaviour e.g. in for loops, where this statement results in an empty > statement, that is > > for (ii in seq(2,5,-2)) print(ii) > > were equivalent to > > for (ii in NULL) print(ii). > > The relevant part in seq.default is now > > if (n < 0) > stop("wrong sign in 'by' argument") > > but could be changed by option to > > return(NULL) > > I think there should be an option to seq requiring this > behaviour, or a > specific function, may be even a special operator, e.g. %;%: > > 3;5 resulting in NULL. > > What do you think? > > Christian > -- > Dr. Christian W. Hoffmann, > Swiss Federal Research Institute WSL > Mathematics + Statistical Computing > Zuercherstrasse 111 > CH-8903 Birmensdorf, Switzerland > > Tel +41-44-7392-277 (office) -111(exchange) > Fax +41-44-7392-215 (fax) > christian.hoffmann at wsl.ch http://www.wsl.ch/staff/christian.hoffmann > > International Conference 5.-7.6.2006 Ekaterinburg Russia > "Climate changes and their impact on boreal and temperate > forests" http://ecoinf.uran.ru/conference/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
On Mon, 27 Mar 2006, Christian Hoffmann wrote:> Hi, > > This may belong more to r-develop, but general discussion may be useful > (for the how many-th time ?)The place for general discussion of changes to R is the R-devel list. There is almost no scope to change things like this, as there is so much existing code which relies on it (and it is also compatible with S).> seq(2,5,-2) > seq(5,2,2) > > both result in > > Error in seq.default(2, 5, -2) : wrong sign in 'by' argument > > But often, if not always, mathematicians and programmers want a > behaviour e.g. in for loops, where this statement results in an empty > statement, that is > > for (ii in seq(2,5,-2)) print(ii) > > were equivalent to > > for (ii in NULL) print(ii). > > The relevant part in seq.default is now > > if (n < 0) > stop("wrong sign in 'by' argument") > > but could be changed by option to > > return(NULL)Why is NULL plausible? I would think integer(0) is more likely, but if you think this should not be an error, then another plausible interpretation is the intersection of {2 ... 5} and {2, 0, -2, ...}, that is {2}. (The only language I can think of with a close analogue is Fortran DO loops, where DO 10 I=2,5,-2 used to be {2} and was defined in F77 to be empty, so I don't think the interpretation is unambiguous.) [As an aside, one could argue that 'for (ii in NULL)' should be an error, since the help page says seq: An expression evaluating to a vector (including a list). and NULL is not a vector.]> I think there should be an option to seq requiring this behaviour, or a > specific function, may be even a special operator, e.g. %;%: > > 3;5 resulting in NULL. > > What do you think?It cannot be the default, and if you need it, why not write your own function to do it? S has survived ca 18 years without it, so the need cannot be overwhelming. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595