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

ripley at stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Jul 27 09:07:00 CEST 2007


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?


On Thu, 26 Jul 2007, bill at insightful.com wrote:

> Full_Name: Bill Dunlap
> Version: 2.5.0
> OS: Linux
> Submission from: (NULL) (70.98.76.47)
>
>
> sequence(nvec) is documented to return
> the concatenation of seq(nvec[i]), for
> i in seq(along=nvec).  This produces inconvenient
> (for me) results for 0 inputs.
>    > sequence(c(2,0,3)) # would like 1 2 1 2 3, ignore 0
>    [1] 1 2 1 0 1 2 3
> Would changing sequence(nvec) to use seq_len(nvec[i])
> instead of the current 1:nvec[i] break much existing code?
>
> On the other hand, almost no one seems to use sequence()
> and it might make more sense to allow seq_len() and seq()
> to accept a vector for length.out and they would return a
> vector of length sum(length.out),
>    c(seq_len(length.out[1]), seq_len(length.out[2]), ...)
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

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



More information about the R-devel mailing list