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

Robin Hankin r.hankin at noc.soton.ac.uk
Fri Jul 27 09:33:18 CEST 2007

On 27 Jul 2007, at 08:07, ripley at stats.ox.ac.uk 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?

My 2 cents:


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

is much faster.

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

Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

More information about the R-devel mailing list