[R] seq(2,5,-2) not an error but NULL

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Mar 27 14:29:18 CEST 2006


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




More information about the R-help mailing list