[Rd] (PR#9811) sequence(c(2, 0, 3)) produces surprising results,

maechler at stat.math.ethz.ch maechler at stat.math.ethz.ch
Fri Jul 27 18:37:47 CEST 2007


>>>>> "Robin" == Robin Hankin <r.hankin at noc.soton.ac.uk>
>>>>>     on Fri, 27 Jul 2007 08:33:18 +0100 writes:

    Robin> On 27 Jul 2007, at 08:07, ripley at stats.ox.ac.uk
    Robin> wrote:

    >> This is as doumented, and I think you could say the same
    >> thing of seq().  BTW, sequence() allows negative inputs,
    >> and I don't think you want sum(input) in that case.
    >> 
    >> I've never seen the point of sequence(), but it has been
    >> around in R for a long time.  It is used in packages eRm,
    >> extRemes, hydrosanity, klaR, seas.  Who knows what people
    >> have in private code, so I don't see any compelling case
    >> to change it.  If people want a different version, it
    >> would only take a minute to write (see below).
    >> 
    >> We could make seq_len take a vector argument, but as you
    >> point out in a followup that makes it slower in the
    >> common case.  It also changes its meaning if a length > 1
    >> vector is supplied, and would speed matter in the
    >> long-vector case?  What does
    >> 
    >> sequence0 <- function (nvec) { s <- integer(0) for (i in
    >> nvec) s <- c(s, seq_len(i)) s }
    >> 
    >> not do that is more than a very rare need?
    >> 


 Robin> My 2 cents:

    Robin>   Defining

    Robin>         mySequence <-
    Robin> function(x){unlist(sapply(x,function(i){seq_len(i)}))}

    Robin> is much faster.

    Robin> Neither sequence0() nor mySequence() accepts vectors
    Robin> with any element <0 although as Brian Ripley points
    Robin> out, sequence() itself does (which I think is
    Robin> undesirable).

Yes, I agree.

Some more historical perspective (Brian alluded to) :

As the third R core member (first after Robert & Ross),
I still have access to the following R version 
{on one very old fortunately still running Solaris machine; I'm
 pretty sure it would not compile anymore on any recent
 OS/compiler suite} :

--------------------------------------------------------------------------
..$ R-0.00alpha

R Alpha-Test Version, Copyright (C) 1995 Robert Gentleman and Ross Ihaka

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type `license()' for details.

> sequence
function (nvec)
{
        sequence <- NULL
        for (i in (1:length(nvec))) sequence <- c(sequence, seq(nvec[i]))
        sequence
}
>                  
--------------------------------------------------------------------------

which interestingly also "works" for negative nvec[i],
but the way it is written even more clearly suggests that
negative nvec  entries were not the intent.

I'm voting that R should adopt a new (fast, but R code
only) version of sequence() which gives an error for negative
'nvec' entries --- though I do agree with Brian that it's not
really an important function at all.

Martin Maechler, ETH Zurich

PS: 
  Note that this was before R became GPL'ed "Free software", and
  that the R version stems from the following place -- back in 1995 :

  /anonymous at stat.auckland.ac.nz:/pub/R/unix/
  -rw-r--r--  1 51           1371 Jun 20  1995 INSTALL
  -rw-r--r--  1 51         466232 Jun 20  1995 R-unix-src.tar.gz
  -rw-r--r--  1 51           1079 Jun 20  1995 README




More information about the R-devel mailing list