[R] generating a sequence

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Dec 21 23:19:44 CET 1999


"Jens Oehlschlägel-Akiyoshi" <jens.oehlschlaegel-akiyoshi at mdfactory.de> writes:

> As this looks like a funny performance competition, 
> here a very tricky algorithm I once received from Angelo Canty
> 
> vecseq <- function(x,y=NULL) {
>     # x1:y1,x2:y2 ... Angelo Canty 8/97
>     # parameter handling (c) JOA 1997
>         if (missing(y)) {y <- x; x <- 1}
>         if (length(y)==1) y <- rep(y,length(x))
>         if (length(x)==1) x <- rep(x,length(y))
>         str <- paste("c(",paste(x,y,sep=":",collapse=","),")")
>         eval(parse(text=str))
> }
....
> So, vecsec() is more flexible and much faster than R's sequence,
> why not redefine sequence ?
> (timings on PII 400 NT4.0)

Hmm. vecseq is certainly impressively fast, but I tend to find any
routine that involves parsing suspicious.

sequence<-function(nvec)unlist(lapply(nvec,function(x)1:x))

clocks in at 0.09 (if you gc() first) and would seem to be equivalent
to the current one. Still two times slower than vecseq, though...

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



More information about the R-help mailing list