Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find consecutive sub-vectors of length k out of a numeric vector which satisfy a given condition

Tags:

r

I have a numeric vector in R, say

v= c(2,3,5,6,7,6,3,2,3,4,5,7,8,9,6,1,1,2,5,6,7,11,2,3,4)

Now, I have to find all the consecutive sub-vector of size 4 out of it with the condition that each element of the sub-vector must be greater than 2 and all sub-vector must be disjoint in the sense that non of the two sub-vector can contain same index element. So my output will be:

(3,5,6,7),(3,4,5,7),(5,6,7,11)

Edited: Other examples for illustration purpose: for,

v=c(3,3,3,3,1,3,3,3,3,3,3,3,3) 

output will be :

(3,3,3,3), (3,3,3,3),(3,3,3,3).

and for,

v= c(2,3,5,5,7,6,3,2,3,4,5,7,8,9,6,1,1,2,5,6,7,11,2,3,4) 

output will be

(3,5,5,7),(3,4,5,7),(5,6,7,11)

The second condition on the output simply says that if we found any sub- array say (v[m],v[m+1],v[m+2],v[m+3]) with each element greater than > 2 then it will goes into my output and the next sub-array can only be start from v[m+4](if possible)

like image 624
rks Avatar asked Dec 16 '25 18:12

rks


1 Answers

This solution uses embed() to create a matrix of lags and then extracts the desired rows from this matrix:

v <- c(2,3,5,6,7,6,3,2,3,4,5,7,8,9,6,1,1,2,5,6,7,11,2,3,4)

e <- embed(v, 4)
ret <- which(
  apply(e, 1, function(x)all(x > 2)) &
  apply(e, 1, function(x)length(unique(x)) == 4)
)
rows <- ret[c(1, 1 + which(diff(ret) > 4))]

e[rows, 4:1]

     [,1] [,2] [,3] [,4]
[1,]    3    5    6    7
[2,]    3    4    5    7
[3,]    5    6    7   11
like image 163
Andrie Avatar answered Dec 19 '25 12:12

Andrie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!