Try this:

which(x %in% max(x))

Then yuo can use, max or tail for get the last value.

On Thu, Jul 2, 2009 at 4:30 PM, Kenny D'Amica <Kenny.DAmica@aier.org> wrote:

> Hello,
>
>
>
> I'm currently writing a program to analyze economic indicators, and need
> to identify the indices of peaks and troughs in certain intervals
> (business cycles) of my time series. I am using which.max() for this,
> which gives me the index of the first max in the interval. However, I
> need a function that returns the last max.
>
>
>
> One workaround I've thought of is to switch the endpoints of my range
> around, i.e.
>
>
>
> Instead of:
>
>
>
> > x <- c(1:4,8,1:3,8,1,2)
>
> > x
>
>  [1] 1 2 3 4 8 1 2 3 8 1 2
>
> > which.max(x[1:11])
>
> [1] 5
>
>
>
> Using:
>
>
>
> > which.max(x[11:1])
>
> [1] 3
>
> > y <- (length(x[11:1])- which.max(x[11:1]) + 1)
>
> > y
>
> [1] 9
>
>
>
> which would return the last max.
>
>
>
> However, I'd rather not add this layer of obfuscation to my code if it
> can be avoided.
>
>
>
> Any suggestions? Thanks in advance for your help.
>
>
>
> -Kenny D'Amica
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]


