Johannes Graumann
2007-Dec-20 21:43 UTC
[R] Efficient way to find consecutive integers in vector?
Hi all, Does anybody have a magic trick handy to isolate directly consecutive integers from something like this: c(1,2,3,4,7,8,9,10,12,13) The result should be, that groups 1-4, 7-10 and 12-13 are consecutive integers ... Thanks for any hints, Joh
Gabor Csardi
2007-Dec-20 22:11 UTC
[R] Efficient way to find consecutive integers in vector?
Joh, x <- c(1,2,3,4,7,8,9,10,12,13) which(diff(x) != 1) gives the indices of the 'jumps'. Gabor On Thu, Dec 20, 2007 at 10:43:05PM +0100, Johannes Graumann wrote:> Hi all, > > Does anybody have a magic trick handy to isolate directly consecutive > integers from something like this: > c(1,2,3,4,7,8,9,10,12,13) > > The result should be, that groups 1-4, 7-10 and 12-13 are consecutive > integers ... > > Thanks for any hints, Joh > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK
Marc Schwartz
2007-Dec-20 22:33 UTC
[R] Efficient way to find consecutive integers in vector?
On Thu, 2007-12-20 at 22:43 +0100, Johannes Graumann wrote:> Hi all, > > Does anybody have a magic trick handy to isolate directly consecutive > integers from something like this: > c(1,2,3,4,7,8,9,10,12,13) > > The result should be, that groups 1-4, 7-10 and 12-13 are consecutive > integers ... > > Thanks for any hints, JohNot fully tested, but here is one possible approach:> Vec[1] 1 2 3 4 7 8 9 10 12 13 Breaks <- c(0, which(diff(Vec) != 1), length(Vec))> Breaks[1] 0 4 8 10> sapply(seq(length(Breaks) - 1),function(i) Vec[(Breaks[i] + 1):Breaks[i+1]]) [[1]] [1] 1 2 3 4 [[2]] [1] 7 8 9 10 [[3]] [1] 12 13 For a quick test, I tried it on another vector: set.seed(1) Vec <- sort(sample(20, 15))> Vec[1] 1 2 3 4 5 6 8 9 10 11 14 15 16 19 20 Breaks <- c(0, which(diff(Vec) != 1), length(Vec))> Breaks[1] 0 6 10 13 15> sapply(seq(length(Breaks) - 1),function(i) Vec[(Breaks[i] + 1):Breaks[i+1]]) [[1]] [1] 1 2 3 4 5 6 [[2]] [1] 8 9 10 11 [[3]] [1] 14 15 16 [[4]] [1] 19 20 HTH, Marc Schwartz
Johannes Graumann
2007-Dec-20 23:00 UTC
[R] Efficient way to find consecutive integers in vector?
Thanks for your Inputs! Joh Johannes Graumann wrote:> Hi all, > > Does anybody have a magic trick handy to isolate directly consecutive > integers from something like this: > c(1,2,3,4,7,8,9,10,12,13) > > The result should be, that groups 1-4, 7-10 and 12-13 are consecutive > integers ... > > Thanks for any hints, Joh > > ______________________________________________ > 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.