[R] seqN

Thomas Lumley thomas at biostat.washington.edu
Tue Nov 30 18:28:22 CET 1999


On Tue, 30 Nov 1999, Paul Gilbert wrote:

>  I have  been using a function
> 
> seqN <- function(N) {if (0==length(N)) NULL else if (N<=0) NULL else seq(N)}
> 
> in for loops when I want no looping in the case of a zero value:
> 
> for (i in seqN(m) ) ...
> 
> Since seqN(0) returns NULL this results in for doing no loops (which is much
> more convenient than always checking for m==0). It seems that a function like
> this should be defined in R (and S) but I have never found it. Is there an
> already defined function that does this (or a better way to achieve this
> result)?

seq(length=m) almost does this.  It doesn't work for negative m, but for
zero or positive m it does the same as your function

R> seq(length=0)
numeric(0)
R> seq(length=1)
[1] 1
R> seq(length=5)
[1] 1 2 3 4 5

(the substitution of numeric(0) for NULL doesn't matter)

	
Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list