Reputation: 65
I have a set of vectors in R
and want to find is there any index for the that the values suddenly decrease after that. for example: [1739,11560,20257,4,2,0,5] in this example the output should be "4".
How can I do this?
Upvotes: 0
Views: 159
Reputation: 887203
We can use diff
to find the difference of adjacent elements in the vector
, convert it to a logical vector with (< 0
) and find the index of the first TRUE value with which.max
which.max(c(FALSE, diff(v1) < 0))
#[1] 4
v1 <- c(1739, 11560, 20257, 4, 2, 0, 5)
Upvotes: 2