Re: [R] Function for describing segements in sequential data

2010-01-27 Thread Steve Lianoglou
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.


Re: [R] Function for describing segements in sequential data

2010-01-27 Thread William Dunlap
 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of Gregory Gentlemen
 Sent: Wednesday, January 27, 2010 9:31 AM
 To: r-help@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@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.
 

__
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.