sequence()
I definitely agree that 'sequence' is not the right name for this functionality. The functionality is occasionally useful -- I've been asked for it several times. But I do wonder if it is basic enough that it should be in 'base'. The function could be rewritten to create the proper length of the answer at the outset since that is known. It could then be used as an example (somewhere) of how not to fragment memory. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User")
Prof Brian Ripley wrote:
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.