[R] Find sequence in vector

Petr Savicky savicky at cs.cas.cz
Sat Apr 7 08:45:23 CEST 2012


On Fri, Apr 06, 2012 at 10:25:15AM -0700, ens wrote:
> > a<-sample(1:6,100,replace=T)
> > a
>   [1] 2 4 3 4 5 1 3 2 4 3 6 6 2 6 2 1 5 5 3 4 6 1 6 6 3 4 6 6 4 4 5 4 6 5 6
> 3 4 5 6 3 4 1 6 6 6 4 2 1 1 3 1 5 3 2 2 6 2 5
>  [59] 2 6 1 6 1 1 6 4 4 2 2 3 4 5 6 1 6 4 6 1 5 1 1 2 1 3 4 4 6 3 1 4 1 1 1
> 5 5 2 4 6 5 1
> which(a<=3)
>  [1]   1   3   6   7   8  10  13  15  16  19  22  25  36  40  42  47  48  49 
> 50  51  53  54  55  57  59  61  63  64  68
> [30]  69  70  74  78  80  81  82  83  84  88  89  91  92  93  96 100
> 
> I want to know if the indices are sequential and if so, how many of them are
> sequential in a row. Does anyone know the least clumsy way to do this. I am
> a C++ user by default, so my instinct is probably too mess for R.

Hi.

Try this.

  set.seed(12345)
  (a<-sample(1:6,100,replace=T))

  [1] 5 6 5 6 3 1 2 4 5 6 1 1 5 1 3 3 3 3 2 6 3 2 6 5 4 3 5 4 2 3 5 1 2 5 3 3 6
 [38] 6 4 1 5 3 6 5 2 2 1 1 1 4 6 5 2 2 5 3 5 1 3 2 5 2 6 5 6 2 6 1 4 6 5 4 3 3
 [75] 1 4 6 4 4 1 6 4 1 1 1 2 5 4 5 1 6 5 1 4 5 4 5 5 1 3

  out <- rle(a <= 3)
  out$lengths[out$values]

   [1] 3 2 6 2 1 2 2 2 1 1 5 2 1 3 1 1 1 3 1 4 1 1 2

The first 3 is due to (3 1 2).
The next 2 is due to (1 1),
The next 6 is due to  (1 3 3 3 3 2).

The starting indices of the blocks are

  c(0, cumsum(out$lengths))[which(out$values)] + 1

   [1]  5 11 14 21 26 29 32 35 40 42 45 53 56 58 62 66 68 73 80 83 90 93 99

The endings are

  cumsum(out$lengths)[which(out$values)]

  [1]   7  12  19  22  26  30  33  36  40  42  49  54  56  60  62  66  68  75  80
  [20]  86  90  93 100

Hope this helps.

Petr Savicky.



More information about the R-help mailing list