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

Liaw, Andy andy_liaw at merck.com
Mon Mar 27 14:26:42 CEST 2006


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
> 
>




More information about the R-help mailing list