Kosar Kazemi
Kosar Kazemi

Reputation: 65

How to find if there is a point in a vector with sudden decrease in value after that in R

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

Answers (1)

akrun
akrun

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

data

v1 <- c(1739, 11560, 20257, 4, 2, 0, 5)

Upvotes: 2

Related Questions