[R] Using seq_len() vs 1:n]

William Dunlap wdunlap at tibco.com
Fri Feb 12 16:59:08 CET 2010


The next version of S+ will have seq_len in
it.  Currently the CSAN package Rcompat contains
it.

Parsing the name seq_len can be a problem, since
S+ 8.1 still allows the use of the underscore
for assignment (it warns about its use, but
allows it).  Either parse your code file in R mode
by calling set.parse.mode("R") or use backticks,
`seq_len`.

By the way, a long time ago I proposed the syntax
    n +: m
to mean the increasing sequence from n up to m,
returning integer(0) if m<n, but no one cared for
the syntax.  (n -: m would make the analogous
decreasing sequence.)  It would make things like
    1 +: n
work like seq_len(n) but also made things like
    curMax <- x[1]
    for(i in 2 +: length(x))
       if (x[i] > curMax)
           curMax <- x[i]
a little simpler to write so it works when length(x)==1.
Currently you might write that for statement as
    if (length(x)>1) for(i in 2:length(x))
or
    for(i in as.integer(seq(from=2L, length=length(x)-1))) # seq(len=)
-> numeric, not integer
or
    for(i in 1L + seq_len(length(x)-1))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Ehlers
> Sent: Friday, February 12, 2010 3:48 AM
> To: r-help at r-project.org
> Subject: Re: [R] Using seq_len() vs 1:n]
> 
> Pat Burns makes a good point. -Peter
> 
> -------- Original Message --------
> Subject: Re: [R] Using seq_len() vs 1:n
> Date: Fri, 12 Feb 2010 09:01:20 +0000
> From: Patrick Burns <pburns at pburns.seanet.com>
> To: Peter Ehlers <ehlers at ucalgary.ca>
> References: <4B746AEF.10900 at ucalgary.ca>
> 
> If you want your code to be compatible with
> S+, then 'seq_len' isn't going to work.
> 
> On 11/02/2010 20:39, Peter Ehlers wrote:
> > R-people,
> >
> > Duncan Murdoch's response in
> >
> > https://stat.ethz.ch/pipermail/r-help/2010-February/227869.html
> >
> > reminded me of something I had been meaning to ask.
> >
> > A while ago I started using
> >
> > for(i in seq_len(v)) {....}
> >
> > in preference to
> >
> > for(i in 1:n) {....}
> >
> > Duncan's post shows that if n can be zero, there is
> > an advantage to using seq_len.
> > Is there ever a *dis*advantage?
> >
> > Peter Ehlers
> > University of Calgary
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> -- 
> Patrick Burns
> pburns at pburns.seanet.com
> http://www.burns-stat.com
> (home of 'The R Inferno' and 'A Guide for the Unwilling S User')
> 
> -- 
> Peter Ehlers
> University of Calgary
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list