Skip to content

seq()

6 messages · jim holtman, Berwin A Turlach, Stephan Kolassa +2 more

#
HI:
Could someone help me with the seq function? I have a range of values starting from 1 to 52 but I want seq to start at 27 by=2, but when it reaches 51 start with with number 1 to 25. is this possible. I can do the basics of seq() but I can't figure how to do this one. This is how I want my sequence to look like:
27 29 31 33 35 37 ............51 1 3 5 7 9 11 13 ...........25

Felipe D. Carrillo  
Supervisory Fishery Biologist  
Department of the Interior  
US Fish & Wildlife Service  
California, USA
#
Is this what you want:
[1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43
45 47 49 51
[1] 27 29 31 33 35 37 39 41 43 45 47 49 51  1  3  5  7  9 11 13 15 17
19 21 23 25
On Wed, Jan 21, 2009 at 12:29 PM, Felipe Carrillo
<mazatlanmexico at yahoo.com> wrote:

  
    
#
G'day Felipe,

On Wed, 21 Jan 2009 09:29:03 -0800 (PST)
Felipe Carrillo <mazatlanmexico at yahoo.com> wrote:

            
Are you after something like:

R> c(seq(27,51,by=2), seq(1,25,by=2))
 [1] 27 29 31 33 35 37 39 41 43 45 47 49 51  1  3  5  7  9 11 13 15 17 19 21 23
[26] 25

or

R> seq(1,52,by=2)[c(14:26,1:13)]
 [1] 27 29 31 33 35 37 39 41 43 45 47 49 51  1  3  5  7  9 11 13 15 17 19 21 23
[26] 25

??

HTH.

Cheers,

	Berwin

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6515 4416 (secr)
Dept of Statistics and Applied Probability        +65 6515 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba
#
Hi Felipe,

concatenate two sequences using c():

c(seq(from=27,to=51,by=2),seq(from=1,to=25,by=2))

HTH,
Stephan


Felipe Carrillo schrieb:
#
On Wed, 2009-01-21 at 09:29 -0800, Felipe Carrillo wrote:
I don't know of a way to do it with one seq() call, without further
processing, but here are two solutions:

First, just concatenate two seq() calls together:

c(seq(27, 51, by = 2), seq(1, 25, by = 2))

which could be wrapped into a function for ease of use:

bar <- function(from, to, mid, by = 2) {
   c(seq(from = mid, to = to, by = by), 
     seq(from = from, to = mid-by, by = by))
}

Alternatively, produce the full sequence and then break if at the point
you want and return the latter half plus the first half:

foo <- function(from, to, mid, by = 2) {
   SEQ <- seq(from = from, to = to, by = by)
   midp <- which(SEQ == mid)
   c(SEQ[midp:length(SEQ)], SEQ[1:(midp-1)])
}

In both cases I take mid to be the point you want to break at, or start
the *final* sequence from.

foo(1, 51, by = 2, mid = 27)

bar(1, 51, by = 2, mid = 27)

HTH

G
#
It works like a charm,thank you all for your help.
--- On Wed, 1/21/09, jim holtman <jholtman at gmail.com> wrote: