peaks: Find Local Maxima

View source: R/peaks.R

peaksR Documentation

Find Local Maxima

Description

Find the local maxima in a vector.

Usage

peaks(x, span = 3, ties = "first", ends = TRUE)

Arguments

x

any numeric vector. Missing values are permitted, but suppress identifying peaks within span.

span

The window width, the default value is 3, meaning compare each value to both of its neighbors. The value for span must be odd and if set to an even value, then it is increased to the next largest odd value.

ties

a character indicating how to handle ties. See Details.

ends

a logical value indicating whether or not to include either the first or last observations in the sequence if it is a local maximum.

Details

Possible values for ties include "none," which treats sequential tied values as individual values; all other values can be thought of as collapsing sequential tied values—"first," "middle," or "last" identify the first, middle, or last, respectively, of a sequence of ties as the peak if appropriate.

Value

A vector matching x of logical values indicating whether the corresponding element is a local maximum or not.

Note

A peak is defined as an element in a sequence that is strictly greater than all other elements within a window of width span centered at that element. As such, setting ties to "none" has the effect of not identifying peaks with sequential tied values.

See Also

max

Examples


# Note the effect of missing values
peaks(c(1:6,5,4,NA,4,6,9,NA))
peaks(c(1:6,NA,5,4,NA,4,6,9,NA))
# Note the effect of ties
peaks(c(1:6,6,6,5,4,3,4,6,9))
peaks(c(1:6,6,6,5,4,3,4,6,9), ties="none")

USGS-R/smwrBase documentation built on Oct. 18, 2022, 9:55 a.m.