[R] generating a sequence

Jens Oehlschlägel-Akiyoshi jens.oehlschlaegel-akiyoshi at mdfactory.de
Tue Dec 21 18:26:53 CET 1999



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

> n <- 500
> 
> system.time({
+ vecseq((n-1):1)
+ })
[1] 0.04 0.00 0.04   NA   NA
> 
> system.time({
+ A <- matrix(1, n-1, n-1)
+ rA <- row(A)
+ rA[rA + col(A) <= n]
+ })
[1] 0.33 0.00 0.33   NA   NA
> 
> system.time({
+ rA <- outer(1:n, 1:n, "-")
+ rA[rA>=1]
+ })
[1] 0.62 0.00 0.62   NA   NA
> 
> system.time({
+ sequence((n-1):1)
+ })
[1] 2.18 0.01 2.27   NA   NA

So, vecsec() is more flexible and much faster than R's sequence,
why not redefine sequence ?
(timings on PII 400 NT4.0)

Regards


Jens Oehlschlaegel-Akiyoshi

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