# Re: [R] Finding local minima and maxima

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 16 Jun 2005 - 09:35:51 EST

1. See ?turnpoints in package pastecs.
2. do it yourself with rapply in the zoo package:

library(zoo)
f <- function(x) x[2] > max(x[-2])
rapply(zoo(rank(x, ties = "first")), 3, function(x) f(x) - f(-x))

Note that if a run is an extrema then the last member of a    run of maxima and the first member of a run of minima will    be identified as the extremum.

3. There are also a number of other solutions on r-help you

could find if you look through the result of the R command:

RSiteSearch("peak")

On 6/15/05, Sean Davis <sdavis2@mail.nih.gov> wrote:
> Thanks, Uwe.
>
> Actually, I didn't mean to make this complicated--simple vector x and
> values at x in a vector y. Just wanted to determine the local minima
> and maxima of y[x], but y is not continuous (but is a "smooth" function
> of x). I will probably use hist or density to do it and then look at
> these curves or cuts.
>
> Sean
>
> On Jun 15, 2005, at 8:16 AM, Uwe Ligges wrote:
>
> > Sean Davis wrote:
> >
> >> I have data in the form of (x,y) pairs and would like to find local
> >> minima and maxima (typically the zeros of the 2nd derivative) of the
> >> y values. I looked at numericDeriv, but I don't have an "expression"
> >> per se. I looked at optim, also, but it looks like it will find only
> >> one "global" max or min. I can code up my own piecewise derivatives,
> >> but wondered if there is an existing function to do this.
> >
> > If you don't have derivatives, and you don't have an expression, and
> > you don't have a criteria how to minimize, please tell me of any known
> > method that can handle your problem well enough.
> >
> > You could look whether y[i-1] <y[i] and y[i+1]<y[i] if y is ordered
> > according to x, if this most trivial and very non-robust method fits
> >
> > Uwe Ligges
> >
> >
> >> Thanks,
> >> Sean
> >> ______________________________________________
> >> R-help@stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help