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

From: Robin Hankin <>
Date: Fri, 27 Jul 2007 08:33:18 +0100

On 27 Jul 2007, at 08:07, 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 (i)}))}

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 mailing list Received on Fri 27 Jul 2007 - 07:37:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 27 Jul 2007 - 17:36:45 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.