EDIT
I think I misunderstood the question earlier (thanks to @thelatemail for bringing that into notice). You want to find out the value which is less than all of next n
values.
You can do this with rolling operations.
n <- 5
which(zoo::rollapply(numbers, n, function(x) all(x >= x[1])))[1]
#[1] 8
Earlier answer
This returns the index of continually increasing sequence in numbers
.
You can use rle
:
n <- 5
with(rle(diff(numbers) > 0),
sum(lengths[seq_len(which(lengths >= n & values)[1] - 1)])) + 1
#[1] 8
You can break it down for better understanding :
diff
gives difference between consecutive numbers.
diff(numbers)
# [1] -0.100 -0.100 -0.100 0.010 -0.010 0.020 -0.030 0.006 0.001
#[10] 0.003 0.010 0.010 -0.170 0.050 0.060 -0.110 0.010 0.020
We compare it with > 0
to get TRUE
for increasing values and FALSE
for decreasing.
diff(numbers) > 0
# [1] FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE
#[12] TRUE FALSE TRUE TRUE FALSE TRUE TRUE
We apply rle
over it :
tmp <- rle(diff(numbers) > 0)
tmp
#Run Length Encoding
# lengths: int [1:10] 3 1 1 1 1 5 1 2 1 2
# values : logi [1:10] FALSE TRUE FALSE TRUE FALSE TRUE ...
We find a position where the length of increasing sequence is greater than equal to n
tmp$lengths >= n & tmp$values
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
Use which
to get it's index, [1]
to select 1st one if there are multiple :
which(tmp$lengths >= n & tmp$values)[1]
[1] 6
sum
all the lengths before this index so -1 to above number
sum(tmp$lengths[seq_len(which(tmp$lengths >= n & tmp$values)) - 1])
#[1] 7
Now add +1 to above number get next index.
If you use this step-by-step approach you could handle different edge cases more easily rather than the one-liner at the top.