[R] Create sequence given start and end vector
Charles C. Berry
cberry at tajo.ucsd.edu
Sun Dec 13 01:01:03 CET 2009
On Sat, 12 Dec 2009, Jorge Ivan Velez wrote:
> Hi Kevin,
>
> Here is a suggestion using mapply():
>
> start <- c(1,10,20)
> end <- c(4,15,27)
> do.call(c, mapply( seq, start, end))
>
...which is what I would usually do.
But for heavy duty applications, the IRanges package and function may be
worth studying:
> require(IRanges) # from bioConductor.org
> as.vector( IRanges( start, end ) )
[1] 1 2 3 4 10 11 12 13 14 15 20 21 22 23 24 25 26 27
>
An introduction to the package is at
http://bioconductor.org/packages/2.5/bioc/vignettes/IRanges/inst/doc/IRangesOverview.pdf
HTH,
Chuck
> See ?mapply and ?do.call for more information.
>
> HTH,
> Jorge
>
>
> On Sat, Dec 12, 2009 at 2:27 PM, Kevin Ummel <> wrote:
>
>> How can I create the following without the 'for' loop?
>>
>> start=c(1,10,20)
>>
>> end=c(4,15,27)
>>
>> out=c()
>>
>> for (i in 1:length(start)) {
>> out=c(out,start[i]:end[i])
>> }
>> out
>> [1] 1 2 3 4 10 11 12 13 14 15 20 21 22 23 24 25 26 27
>>
>> I know there must be an easier (and, hopefully, faster) way.
>>
>> Many thanks in advance,
>> Kevin Ummel
>>
>> Central European University
>> Department of Environmental Science and Policy
>>
>> ______________________________________________
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
More information about the R-help
mailing list