Skip to content

@jamiefolson /peaks.R
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Finding local maxima and minima in R
#' Find local maxima and minima in R
#' @param x numeric vector to search for peaks
#' @param partial whether or not to include partial peaks
#' at the beginning or end of the vector
#' @param decreasing whether to find peaks (the default)
#' or valleys
which.peaks <- function(x,partial=TRUE,decreasing=FALSE){
if (decreasing){
if (partial){
which(diff(c(TRUE,diff(x)<=0,FALSE))>0)
}else {
which(diff(diff(x)<=0)>0)
}
}else {
if (partial){
which(diff(c(TRUE,diff(x)>=0,FALSE))<0)
}else {
which(diff(diff(x)>=0)<0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.