# Determine if value is exceeded n times in a row

#### cmc0605

##### New Member
Hello,

I have a question that I'm not sure how to tackle. Suppose I have a time-series of length 12,000 and would like to know where in the time-series that a prescribed threshold value is exceeded, say, 10 times in a row. That is, for example, if the time series has y values that range from -1 to 1, and I want to determine when the value 0.5 is exceeded 10 times in a row...I'd like to not only know how many times this occurs, but where in the time-series it occurs (say for example by displaying the starting date in which this criteria is satisfied).

I know how to find the locations where the value exceeds a certain number, for example using data.frame, but I have no idea how to make the leap to what I want to do above...aside from combing through the data manually.

#### Jake

This basic approach will work:
Code:
> # our values
> x <- runif(20)*2 - 1
> x
[1]  0.2995647 -0.7860408 -0.1808310  0.8456620 -0.2870231  0.9365280 -0.5220474
[8] -0.2433188  0.8775344 -0.9427301  0.4042524  0.2905310  0.4469883  0.7357491
[15] -0.4565306 -0.6614139  0.6611550 -0.6935338 -0.6498537 -0.2300904
>
> # which elements exceed .5?
> which(x > .5)
[1]  4  6  9 14 17
>
> # get position where .5 is exceeded for the 4th time
> which(x > .5)[4]
[1] 14
>
> # how many more times is it exceeded after that?
> sum(x > .5) - 4
[1] 1
However you may want to extend this slightly to do more fancy things like first check to make sure the target value is exceeded the desired number of times. Not sure what would suit your needs.