I have a vector v. I would like to find the index of the minimum difference of the elements of a vector v which is less that 1e-2. How can I get the index?
v = c(0.0002873771, 0.0006478544, 0.0092186701, 0.0267084167,
0.0457307072 , 0.3176459806)
D = abs(diff(v)) < 1e-2
I want to get that the index = 1 not 1 and 2, just the minimum one.
If we have this vector
v = c( 0.01144003, 0.04644231, 0.05527114, 0.31680614)
D = abs(diff(v)) < 1e-2
index = which.min(abs(diff(v)) < 1e-2 )
It give me the index = = 1 but actually my desire index is 2 not 1.
If you know that the minimum absolute difference is less than your tolerance, which.min is the correct function, and there is no need for
D.This code, in your edited question:
is taking the which.min of a logical vector. Not what you intend. This is what you (probably) want:
To actually check for your tolerance, you can use
anyandif. Here, I assume you wantNAto be returned if all differences exceed your tolerance: