From ripley at stats.ox.ac.uk  Fri Jul 22 13:48:14 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 22 Jul 2005 12:48:14 +0100 (BST)
Subject: [Rd] [R] sequence()
In-Reply-To: <7CBD9E72-0812-47A5-8803-34FFDAB37274@soc.soton.ac.uk>
References: <7CBD9E72-0812-47A5-8803-34FFDAB37274@soc.soton.ac.uk>
Message-ID: <Pine.LNX.4.61.0507221011100.28301@gannet.stats>

R-help is not the list for R development questions: you didn't want help 
did you? --> moved to R-devel.

I do wonder why

> sequence(c(0,-1))
[1]  1  0  1  0 -1

is considered useful.

Given that the definition seems flawed and I could not find any uses of 
this function in any package my reaction was to suggest the function be 
deprecated on the way to removal.  (I also do not understand why anyone 
would expect sequence() to do that and not one of the many things which 
seq() does.)

We certainly do not want to replace a function that works as described at 
a reasonable speed by one that does not work as described, however fast.

 	`Accuracy first, speed second'.


On Fri, 22 Jul 2005, Robin Hankin wrote:

> Function sequence() repeatedly concatenates
> its output, and this is slow.

> It is possible to improve on the performance of sequence by
> defining
>
>  myseq <- function(x){unlist(sapply(x,function(i){1:i}))}

I don't think you want sapply here, but lapply.  Try

> myseq(c(2,2))
      [,1] [,2]
[1,]    1    1
[2,]    2    2

sic!

> The following session compares the  performance of
> myseq(), and sequence(), at least on my G5:
>
>
> > identical(sequence(1:50),myseq(1:50))
> [1] TRUE
> > system.time(ignore <- sequence(1:800))
> [1] 1.16 0.88 2.07 0.00 0.00
> > system.time(ignore <- sequence(1:800))
> [1] 1.14 0.84 1.99 0.00 0.00
> > system.time(ignore <- myseq(1:800))
> [1] 0.02 0.02 0.04 0.00 0.00
> > system.time(ignore <- myseq(1:800))
> [1] 0.03 0.00 0.03 0.00 0.00
> >
>
> (the time differential is even more marked for longer arguments).

and much less for more realistic shorter arguments.

> Is there any reason why we couldn't use  this definition instead?

The fact that it sometimes gives the wrong answer, for one.

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] [R] sequence()
Prof Brian Ripley