[R] Function for describing segements in sequential data
Charles C. Berry
cberry at tajo.ucsd.edu
Wed Jan 27 20:36:48 CET 2010
On Wed, 27 Jan 2010, Gregory Gentlemen 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? I know the rle function will report the length of the
> segments but not the endpoints.
If this is more than a small one-off problem you might try this:
> require(IRanges) # from BioConductor
> IRanges(Rle(x)==1) # n.b. Rle != rle
IRanges of length 3
start end width
[1] 1 3 3
[2] 8 10 3
[3] 15 17 3
>
HTH,
Chuck
>
> Thanks in advance.
>
> Gregory Gentlemen
>
>
>
> __________________________________________________________________
> [[elided Yahoo spam]]
>
>
> [[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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list