Reputation: 51
Given a single vector, I would like to find the longest run, which meets this: the count is being stopped when there is a run of x>1 for the first time, which has got length of at least 5. For example, I got a vector X:
X <- c(2,3,4,0,1,0,0,0,3,2,2,0,3,3,3,3,3,0,0,0)
My desired run has got length of 12, its end is the beggining of the first run of x>1 which is at least 5 numbers long. I know my question is not asked in the most aesthetic way, but I think that I explained it sufficiently.
Upvotes: 3
Views: 104
Reputation: 389175
Maybe you are looking for this -
with(rle(X > 1), {
val <- max(lengths[values & lengths >= 5])
inds <- which(values & lengths == val) - 1
cumsum(lengths)[inds]
})
#[1] 12
Upvotes: 1