[R] Finding indices where

Ravi Varadhan rvaradha at jhsph.edu
Wed May 19 19:26:04 CEST 2004


Hi:

Suppose I have the following vector:

> x <- c(1,4:8,11,13:14,17,19,23:28,35:38)
> x
 [1]  1  4  5  6  7  8  11 13 14 17 19 23 24 25 26 27 28 35 36 37 38
> 

and I would like to pick out the first and last indices of all the 
consecutive "runs" of integers, where the length of a run is no smaller 
than a specified value, say, nmin.  That is, in the above example, for 
nmin=4, I would like to get the following 3 by 2 matrix:

4 8
23 28
35 38

For nmin=5, I would get the following 2 by 2 matrix

4 8
23 28

and for nmin=6, I would get the following 1 by 2 matrix

23 28

Is there an efficient and elegant way to do this?

Thanks very much for any help/suggestions.
Ravi.




More information about the R-help mailing list