OK, so we have now seen essentially three solutions:
Me:
A <- matrix(1, n-1, n-1)
rA <- row(A)
rA[rA + col(A) <= n]
Jonathan Rougier:
rA <- outer(1:n, 1:n, "-")
rA[rA>=1]
Andy Royle:
sequence((n-1):1)
Timings for n=500 on a 1994 Sun Sparc 20:
3.29, 4.68 and 23.86 seconds.
I deliberately did not use outer, as it calls apply and
is normally quite slow. row and col are internal operations and fast.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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, 21 Dec 1999 15:23:49 +0000 (GMT), Prof Brian Ripley <ripley at stats.ox.ac.uk> said:
BDR> OK, so we have now seen essentially three solutions:
BDR> Me:
BDR> A <- matrix(1, n-1, n-1)
BDR> rA <- row(A)
BDR> rA[rA + col(A) <= n]
BDR> Jonathan Rougier:
BDR> rA <- outer(1:n, 1:n, "-")
BDR> rA[rA>=1]
BDR> Andy Royle:
BDR> sequence((n-1):1)
BDR> Timings for n=500 on a 1994 Sun Sparc 20:
BDR> 3.29, 4.68 and 23.86 seconds.
BDR> I deliberately did not use outer, as it calls apply and
BDR> is normally quite slow. row and col are internal operations and fast.
Thanks a lot.
I hope this reminds more people than just me
that we (R users) "desperately" need someone who
rewrites apply(),lapply(), and tapply()
to become much faster (as they are with S-plus [34].y, but not 5.[12])...
Anyone working on this ?
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._