Using rle() may make it easier - finding the peak values is easier and select from cumsum(lengths) to get the positions of the last values before the peaks, then add 1. I have not tested the following very much.
function(x) { rx <- rle(x) cumsum(rx$lengths)[c(diff(diff(rx$values)>0) == -1,FALSE,FALSE)]+1 } -Bill On Fri, Mar 26, 2021 at 8:36 AM Stefano Sofia <stefano.so...@regione.marche.it> wrote: > > Dear list users, > I need to find local maxima and local minima positions in a vector where > there might be duplicates; in the particular in case of > - duplicated local maxima, I should take the position of the first duplicated > value; > - duplicated local minima, I should take the position of the last duplicated > value. > > Example: > > >x <- c(1,0,0,0,2,2,3,4,0,1,1,0,5,5,5,0,1) > >which(diff(diff(x)>=0)<0)+1 > > gives me 8 11 15 while I need 8 10 13; > > >which(diff(diff(x)>0)>0)+1 > > gives me 4 6 9 12 16 while I need 4 9 12 16. > > Could you please help me in this task? I would be happier not to use > additional packages. > > Thank you for your precious help > Stefano > > > (oo) > --oOO--( )--OOo-------------------------------------- > Stefano Sofia PhD > Civil Protection - Marche Region - Italy > Meteo Section > Snow Section > Via del Colle Ameno 5 > 60126 Torrette di Ancona, Ancona (AN) > Uff: +39 071 806 7743 > E-mail: stefano.so...@regione.marche.it > ---Oo---------oO---------------------------------------- > > ________________________________ > > AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere > informazioni confidenziali, pertanto è destinato solo a persone autorizzate > alla ricezione. I messaggi di posta elettronica per i client di Regione > Marche possono contenere informazioni confidenziali e con privilegi legali. > Se non si è il destinatario specificato, non leggere, copiare, inoltrare o > archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, > inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio > computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso > di necessità ed urgenza, la risposta al presente messaggio di posta > elettronica può essere visionata da persone estranee al destinatario. > IMPORTANT NOTICE: This e-mail message is intended to be received only by > persons entitled to receive the confidential information it may contain. > E-mail messages to clients of Regione Marche may contain information that is > confidential and legally privileged. Please do not read, copy, forward, or > store this message unless you are an intended recipient of it. If you have > received this message in error, please forward it to the sender and delete it > completely from your computer system. > > -- > Questo messaggio stato analizzato da Libraesva ESG ed risultato non infetto. > This message was scanned by Libraesva ESG and is believed to be clean. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.