[R] How to find series of small numbers in a big vector?

Michael Dewey info at aghmed.fsnet.co.uk
Tue Jan 30 13:19:47 CET 2007


At 01:49 30/01/2007, Ed Holdgate wrote:

>Hello:
>
>I have a vector with 120,000 reals
>between 0.00000 and 0.9999
>
>They are not sorted but the vector index is the
>time-order of my measurements, and therefore
>cannot be lost.
>
>How do I use R to find the starting and ending
>index of ANY and ALL the "series" or "sequences"
>in that vector where ever there are 5 or more
>members in a row between 0.021 and 0.029 ?

You could look at rle which codes into runs


>For example:
>
>search_range <- c (0.021, 0.029) # inclusive searching
>search_length <- 5   # find ALL series of 5 members within search_range
>my_data <- c(0.900, 0.900, 0.900, 0.900, 0.900,
>              0.900, 0.900, 0.900, 0.900, 0.900,
>              0.900, 0.028, 0.024, 0.027, 0.023,
>              0.022, 0.900, 0.900, 0.900, 0.900,
>              0.900, 0.900, 0.024, 0.029, 0.023,
>              0.025, 0.026, 0.900, 0.900, 0.900,
>              0.900, 0.900, 0.900, 0.900, 0.900,
>              0.900, 0.900, 0.900, 0.900, 0.022,
>              0.023, 0.025, 0.333, 0.027, 0.028,
>              0.900, 0.900, 0.900, 0.900, 0.900)
>
>I seek the R program to report:
>start_index of 12 and an end_index of 16
>-- and also --
>start_index of 23 and an end_index of 27
>because that is were there happens to be
>search_length numbers within my search_range.
>
>It should _not_ report the series at start_index 40
>because that 0.333 in there violates the search_range.
>
>I could brute-force hard-code an R program, but
>perhaps an expert can give me a tip for an
>easy, elegant existing function or a tactic
>to approach?
>
>Execution speed or algorithm performance is not,
>for me in this case, important.  Rather, I
>seek an easy R solution to find the time windows
>(starting & ending indicies) where 5 or more
>small numbers in my search_range were measured
>all in a row.
>
>Advice welcome and many thanks in advance.
>
>Ed Holdgate

Michael Dewey
http://www.aghmed.fsnet.co.uk



More information about the R-help mailing list