[R] seq()

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jan 21 18:54:33 CET 2009


On Wed, 2009-01-21 at 09:29 -0800, Felipe Carrillo wrote:
> 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

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

> Felipe D. Carrillo  
> Supervisory Fishery Biologist  
> Department of the Interior  
> US Fish & Wildlife Service  
> California, USA
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090121/4ae4c159/attachment.bin>


More information about the R-help mailing list