[R] Function for describing segements in sequential data

William Dunlap wdunlap at tibco.com
Wed Jan 27 18:46:28 CET 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Gregory Gentlemen
> Sent: Wednesday, January 27, 2010 9:31 AM
> To: r-help at r-project.org
> Subject: [R] Function for describing segements in sequential data
> 
> 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.

You can use expressions based on cumsum(rle(x)$lengths) or, more
directly, on the following functions 
   isFirstInRun <- function(x)c(TRUE, x[-1]!=x[-length(x)])
   isLastInRun <- function(x)c(x[-1]!=x[-length(x)], TRUE)
which do part of what rle() does.  E.g.,
   > which(isFirstInRun(x) & x==1) # starting positions of runs of 1's
   [1]  1  8 15
   > which(isLastInRun(x) & x==1) # ending positions of runs of 1's
   [1]  3 10 17

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
 
> 
> 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.
> 



More information about the R-help mailing list