Hallo Everybody Consider the following vector a=1:10 b=20:30 c=40:50 x=c(a,b,c) I need a function that can tell me that there are three set of continuos sequences and that the first is from 1:10, the second from 20:30 and the third from 40:50. In other words: a,b, and c. regards Christiaan [[alternative HTML version deleted]]
On Jun 1, 2011, at 6:27 AM, christiaan pauw wrote:> Hallo Everybody > > Consider the following vector > > a=1:10 > b=20:30 > c=40:50 > x=c(a,b,c) > > I need a function that can tell me that there are three set of > continuos > sequences and that the first is from 1:10, the second from 20:30 and > the > third from 40:50. In other words: a,b, and c.You probably want something like > which(diff(x) >1) [1] 10 21 Or perhaps what the rle function provides. ?diff ?rle -- David Winsemius, MD West Hartford, CT
Something like this? a=1:10 b=20:30 c=40:50 x=c(a,b,c) borders <- which(diff(x) != 1) seqs <- data.frame(start = c(1, borders + 1), end = c(borders, length(x))) Best regards, Thierry> -----Oorspronkelijk bericht----- > Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > Namens christiaan pauw > Verzonden: woensdag 1 juni 2011 12:27 > Aan: r-help at r-project.org > Onderwerp: [R] Identifying sequences > > Hallo Everybody > > Consider the following vector > > a=1:10 > b=20:30 > c=40:50 > x=c(a,b,c) > > I need a function that can tell me that there are three set of continuos > sequences and that the first is from 1:10, the second from 20:30 and the third > from 40:50. In other words: a,b, and c. > > regards > Christiaan > > [[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.
I am assuming in this case that you are looking for continuity along integers, so if you expect noninteger values this will not work. You can get the index of where breaks can be found in your example using which(diff(x) > 1) On Wed, Jun 1, 2011 at 6:27 AM, christiaan pauw <cjpauw at gmail.com> wrote:> Hallo Everybody > > Consider the following vector > > a=1:10 > b=20:30 > c=40:50 > x=c(a,b,c) > > I need a function that can tell me that there are three set of continuos > sequences and that the first is from 1:10, the second from 20:30 and the > third from 40:50. In other words: a,b, and c. > > regards > Christiaan > > ? ? ? ?[[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. >-- ==============================================Jon Daily Technician ==============================================#!/usr/bin/env outside # It's great, trust me.