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)? Paul Gilbert -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Paul Gilbert <pgilbert at bank-banque-canada.ca> writes:> 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)?Well, not quite, but:> seq(length=1)[1] 1> seq(length=0)numeric(0)> seq(length=3)[1] 1 2 3> seq(length=-1)Error: Length must be non-negative number -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._