I'm using R to analyze some spectra and I'm trying to get the local maxima, namely their position and their value.
For example, with a vector:
spectrum <- c(1,1,2,3,5,3,3,2,1,1,5,6,9,5,1,1)
I would like the following result:
pos.peaks = c(5,13)
val.peaks = c(5,9)
I've already used the solution provided here: Finding local maxima and minima for the position of the peaks but how do I extract the corresponding value afterwards? Knowing that I don't have just one vector, I have several columns within several dataframes within a list, and I want to apply the function to every single column of all dataframes in the list. For example, for all the positions I did this:
example <- lapply(mylist, function (x) lapply(x, function(y) which(diff(sign(diff(y)))==-2)+1))
I didn't manage to make it work with slice or filter, because I don't need the same rows within the same dataframe...
Furthermore, I would like to know how to reduce the amount of local maxima I get because my data is very noisy.
I'd appreciate any help you can give me.
Thanks!
Nath