Hi, On Wed, Jan 27, 2010 at 12:31 PM, Gregory Gentlemen <gregory_gentle...@yahoo.ca> wrote: > Dear R-users, > > Say that I have a sequence of zeroes and ones: > > x <- c(1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0) > > The sequences of ones represent segments and I want to report the starting > and endpoints of these segments. For example, in 'x', the first segment > starts at location 1 and ends at 3, and the second segment starts at location > 8 and ends at location 10. Is there an efficient way of doing this in R > without having to right a bunch of if-else conditions?
How about something like this: start <- which(diff(c(0,x)) == 1) ## Append first 0 for a "bookend" end <- which(diff(x) == -1) start is: 1, 8, 15 end is: 3, 10, 17 -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact ______________________________________________ R-help@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.