Reputation: 7435
How would you find the last lowest value in this vector with a certain tolerance without changing the order?
Example:
c(0, 785, 10273, 6231, 5417, 2328, 5249, 1725, 2656, 6258, 2687,
2651, 1063, 325, 2556, 738, 631, 140, 57, 1173, 407, 225, 135,
69, 81, 21, 16, 3, 0, 26, 1, 2, 0, 1, 2, 1, 1, 0, 0, 3, 1, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0)
Assume a tolerance of 26. Working backwards from the last element (0) I would like to return the position of the number with tolerance greater than 26 from the previous number. In this example it would be position 30, or the number 26.
Upvotes: 2
Views: 111
Reputation: 93823
You can use Position
with the right=TRUE
argument to avoid having to search forwards and then take the last result:
Position(identity, diff(x) >= 26, right=TRUE) + 1
#[1] 30
Upvotes: 5
Reputation: 35314
If I understand your question correctly, you can do this:
tail(which(diff(x)>=26),1L)+1L;
## [1] 30
Data
x <- c(0,785,10273,6231,5417,2328,5249,1725,2656,6258,2687,2651,1063,325,2556,738,631,140,57,
1173,407,225,135,69,81,21,16,3,0,26,1,2,0,1,2,1,1,0,0,3,1,0,0,0,1,0,0,0,0,1,0);
Upvotes: 2