Last chance! 50% off unlimited learning
Sale ends in
Finds the local maxima in a vector, or time series, or in each column of a matrix.
peaks(x, span=3, strict=TRUE, endbehavior=0)
an object like x
of logical values.
Values that are TRUE correspond to local peaks in the data.
vector or matrix.
a peak is defined as an element in a sequence which is greater than all other elements within a window of width span centered at that element. The default value is 3, meaning that a peak is bigger than both of its neighbors. Default: 3.
logical flag: if TRUE, an element must be strictly greater than all other values in its window to be considered a peak. Default: TRUE.
a value of 0, 1 or 2 that determines how peaks are computed within
a halfwidth
(= floor(span/2)
) of the ends of the sequence.
A value of 0 means no value within halfwidth
of the start or end
can be considered a peak.
A value of 1 means maximum values within halfwidth
of the start or end
can be considered a peak.
A value of 2 returns NA
for the values within halfwidth
of the start or end.
The default is endbehavior=0)
.
x <- as.vector(sunspots)
z <- peaks(x, span=51)
plot(x, type="l")
abline(v=which(z), col="red", lty="dashed")
Run the code above in your browser using DataLab