[R] sequence with start and stop positions

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Aug 26 07:55:11 CEST 2008


There's a kind of way, but it may be more 'elegant' than fast for large sequences

> start <- c(1,20,50)
> stop <- c(7,25,53)
> unlist(mapply(Vectorize(seq), start, stop))
 [1]  1  2  3  4  5  6  7 20 21 22 23 24 25 50 51 52 53

Bill Venables
http://www.cmis.csiro.au/bill.venables/


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Chris Oldmeadow
Sent: Tuesday, 26 August 2008 2:42 PM
To: r-help at r-project.org
Subject: [R] sequence with start and stop positions

Hi,

I have a vector of start positions, and another vector of stop positions,

eg start<-c(1,20,50)
     stop<-c(7,25,53)

Is there a quick way to create a sequence from these vectors?

new<-c(1,2,3,4,5,6,7,20,21,22,23,24,25,50,51,52,53)

the way Im doing it at the moment is

pos<-seq(start[1],stop[1])

for (i in 2:length(start)){
  new<-seq(start[i],stop[i])
  pos<-c(pos,new)
}

This works on small data, but its very inefficient on large vectors, and
is taking forever!

Anybody no a better way?

many thanks,
Chris

______________________________________________
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