Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge two lists in r to make a list of ranges?

I have lists in R as follows:

start <- c(1, 5, 11)
end <- c(2, 9, 13)

how do I concatenate them to make a list of non-contiguous ranges in this manner:

c(1:2, 5:9, 11:13)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try Map

> Map(`:`, start, end)
[[1]]
[1] 1 2

[[2]]
[1] 5 6 7 8 9

[[3]]
[1] 11 12 13

or with unlist in addition

> unlist(Map(`:`, start, end))
 [1]  1  2  5  6  7  8  9 11 12 13

A more efficient way might be using sequence

> sequence(end - start + 1, start)
 [1]  1  2  5  6  7  8  9 11 12 13
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading