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

From: Robin Hankin <r.hankin_at_noc.soton.ac.uk>
Date: Fri, 27 Jul 2007 08:33:18 +0100

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:

  Defining

        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
undesirable).

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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel 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 https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.