[R] Function for describing segements in sequential data

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Jan 27 18:48:22 CET 2010


Hi,

On Wed, Jan 27, 2010 at 12:31 PM, Gregory Gentlemen
<gregory_gentlemen at 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



More information about the R-help mailing list