I am trying to identify local maxima in a data set and am using the following code that I found from LyzandeR:
library(data.table)
maximums <- function(x) which(x - shift(x, 1) > 0 & x - shift(x, 1, type='lead') > 0)
maximums(data$Y)
Doing so gives me the correct maxima values for non-noisy data, but for noisy data it incorrectly (for my purposes, it's working as written) identifies small fluctuations as independent peaks. I've been unable to determine how to extend the range for the maximum, i.e. in order for a point to be considered a maximum, it needs to be greater than, say, 5 adjacent values. Is that possible given this code?
Thank you!