Lukas
Lukas

Reputation: 727

find first match looking back from element in vector r

I have a time series where I identified the local maxima. Now I want to find the indices of the first elements with a value of zero looking back from the maxima. E.g.

my.seq <- c(sin(0:(2*pi)), rep(0, 5), sin(seq(0, (2*pi), 0.5)))

local maxima:

indx.max <- c(3, 16)

I want to get:

c(1, 13)

Thanks

Upvotes: 0

Views: 66

Answers (1)

nicola
nicola

Reputation: 24480

You can try:

x<-which(my.seq==0)
x[findInterval(indx.max,x)]
#[1]  1 13

Upvotes: 3

Related Questions