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
Bill.Venables at csiro.au
2008-Aug-26 05:55 UTC
[R] sequence with start and stop positions
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.
Try:> unlist(mapply(seq, c(1,20,50), c(7,25,53)))[1] 1 2 3 4 5 6 7 20 21 22 23 24 25 50 51 52 53 -Christos> -----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, August 26, 2008 12:42 AM > 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. > >
And somewhat faster still (YMMV): unlist(mapply(":", start, stop)) HTH, Ray Brownrigg Victoria University of Wellington Christos Hatzis wrote:> Try: > >> unlist(mapply(seq, c(1,20,50), c(7,25,53))) > [1] 1 2 3 4 5 6 7 20 21 22 23 24 25 50 51 52 53 > > -Christos > >> -----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, August 26, 2008 12:42 AM >> 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. >> >> > > ______________________________________________ > 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.
On Tue, 26 Aug 2008, Chris Oldmeadow wrote:> 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)Vectorize the process.> start2 <- rep( start, stop-start+1 ) > lens <- stop-start+1 > offset <- rep(cumsum(c( 0, lens )),c( lens ,0 )) > seq(along=offset)-offset+start2-1[1] 1 2 3 4 5 6 7 20 21 22 23 24 25 50 51 52 53>HTH, Chuck> > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Apparently Analagous Threads
- sample consecutive integers efficiently
- sliding window over a large vector
- vectorized sub, gsub, grep, etc.
- ?to calculate sth for groups defined between points in one variable (string), / value separating/ spliting variable into groups by i.e. between start, NA, NA, stop1, start2, NA, stop2
- Matrix operations in a list