[R] grouping by consecutive integers

Carlos J. Gil Bellosta cgb at datanalytics.com
Mon Jul 24 18:10:02 CEST 2006


Dear Kevin,

Try something like

groups <- cut( tmp, c(-Inf, which(diff(tmp) > 1 ) + 0.5, Inf) )

Sincerely,

Carlos J. Gil Bellosta
http://www.datanalytics.com
http://www.data-mining-blog.com

Quoting Kevin J Emerson <kemerson at uoregon.edu>:

> Hello R-helpers!
>
> I have a question concerning extracting sequence information from a
> vector.  I have a vector (representing the bins of a time series where
> the frequency of occurrences is greater than some threshold) where I
> would like to extract the min, median and max of each group of
> consecutive numbers.
>
> For Example:
>
> tmp <- c(24,25,29,35,36,37,38,39,40,41,42,43,44,45,46,47,68,69,70,71)
>
> I would like to have the max,min,median of the following groups:
>
> 24,25
> 29
> 35,36,37,38,39,40,41,42,43,44,45,46,47,
> 68,69,70,71
>
> I would like to be able to perform this for many time series so an
> automated process would be nice.  I am hoping to use this as a peak
> detection protocol.
>
> Any advice would be greatly appreciated,
> Kevin
>
> -----
> -----
> Kevin J Emerson
> Center for Ecology and Evolutionary Biology
> 1210 University of Oregon
> Eugene, OR 97403
> USA
> kemerson at uoregon.edu
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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