[R] Extracting Sequence Data from a Vector

baptiste auguie ba208 at exeter.ac.uk
Wed Jun 10 19:46:11 CEST 2009


jim holtman wrote:
> try this:
>
>   

Oh well, i spent the time writing this so i might as well post my 
(almost identical) solution,

x<-c(1:3, 6: 7, 10:13)

breaks = c(TRUE, diff(x) != 1)

data.frame(start = x[breaks], length = tabulate(cumsum(breaks)))

Hoping this works,

baptiste


>> x
>>     
> [1]  1  2  3  6  7 10 11 12 13
>   
>> # find breaks
>> breaks <- c(FALSE, diff(x) != 1)
>> # now create matrix with groupings (just for visual)
>> z <- data.frame(x, cumsum(breaks))
>> # create list with first element of each seq and the length
>> t(sapply(split(z, z[,2]), function(b) c(b[1,1], nrow(b))))
>>     
>   [,1] [,2]
> 0    1    3
> 1    6    2
> 2   10    4
>
>
> On Wed, Jun 10, 2009 at 1:28 PM, Eric Vander Wal <eric.vanderwal at usask.ca>wrote:
>
>   
>> Thanks in advance.
>> I have a vector of numbers which contain sections that are sequences which
>> increase by a value of 1 followed by a gap in the data and then another
>> sequence occurs, etc:
>>
>> x<-c(1:3, 6: 7, 10:13)
>>
>> From the vector I need to extract 2 items of information A) the first
>> number in the sequence (e.g., 1, 6, 10) and B) how many observations were in
>> each  sequence section (e.g., 3, 2, 4).
>>
>> v1   v2
>> 1      3
>> 6      2
>> 10    4
>>
>> It seems simple, but my R skills are still in their infancy so I very much
>> appreciate the help.
>> Eric
>>
>> --
>> Eric Vander Wal
>> Ph.D. Candidate
>> University of Saskatchewan, Department of Biology,
>> 112 Science Place, Saskatoon, SK., S7N 5E2
>>
>> "Pluralitas non est ponenda sine neccesitate"
>>
>> ______________________________________________
>> 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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>     
>
>
>
>




More information about the R-help mailing list