s-news
[Top] [All Lists]

Re: abusing seq

To: "Joseph S. Verducci" <jsv@stat.ohio-state.edu>
Subject: Re: abusing seq
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Date: Sun, 29 Jul 2001 20:21:54 +0100 (BST)
Cc: <s-news@wubios.wustl.edu>
In-reply-to: <Pine.SOL.4.33.0107291215370.27133-100000@jove.stat.ohio-state.edu>
On Sun, 29 Jul 2001, Joseph S. Verducci wrote:

>
> Dear Sers:
>
>    In a blatant abuse of an innocent and well-respected function,
> I tried the following command:
>
>    seq(from=2,by=c(1,2),length=20)
>
> which resulted in
>
>  [1]  2  4  4  8  6 12  8 16 10 20 12 24 14 28 16 32 18 36 20 40
>
> Even though I don't remember seeing this sequence in an IQ test,
> the following rule seems to work {with an abuse of Splus notation}:
>   a[odd] = seq(from=2,by=2)
>   a[even] = seq(from=4,by=4)
>
>
> Similarly,
>
>   seq(from=1,by=c(2,3),length=20)
>
> results in
>
>  [1]  1  4  5 10  9 16 13 22 17 28 21 34 25 40 29 46 33 52 37 58
>
> with
>   a[odd] = seq(from=1,by=4)
>   a[even] = seq(from=4,by=6)
>
> Can anyone explain the general behavior of "seq" when "by" is given a
> vector argument?

The crucial lines are (S-PLUS 3.4 at least)

        else if(missing(to))
                from + (0:(length.out - 1)) * by

so in your first example

> 2 + (0:(20-1))*c(1,2)
 [1]  2  4  4  8  6 12  8 16 10 20 12 24 14 28 16 32 18 36 20 40

You might now see the pattern (40 pairs)

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


<Prev in Thread] Current Thread [Next in Thread>
  • abusing seq, Joseph S. Verducci
    • Re: abusing seq, Prof Brian Ripley <=