Thank you Bill, your tip solved everything.

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----------------------------------------

________________________________________
Da: Bill Dunlap [williamwdun...@gmail.com]
Inviato: lunedì 29 marzo 2021 17.22
A: Stefano Sofia
Cc: bgunter.4...@gmail.com; minsh...@umich.edu; r-help mailing list
Oggetto: Re: [R] local maxima positions in a vector with duplicated values

If you want to accept maxima at the ends of the series, append -Inf to
each end and subtract one from the result.  Note that this will make
even a constant sequence have a local maximum at 1.

On Mon, Mar 29, 2021 at 6:52 AM Stefano Sofia
<stefano.so...@regione.marche.it> wrote:
>
> Dear Bill, Bert, Greg and Abby,
> I tested your code (honestly apart from Abby's solution because it needed an 
> additional package and I run R in a server which is not administrated by 
> myself), they work and I found them equivalent. (Probably Bill's and Greg's 
> solutions are a bit faster than Bert's.)
>
> In my real application I found a case where none of them work: when the 
> series is monotonic. In this case the last number of the vector (or the last 
> duplicated numbers) is not a maximum but it is anyway the highest number of 
> the vector:
>
> >x <- c(1,1,1,2,2,3,4,4,4,5,6,6,6)
>
> The error is: argument is of length zero.
> Your code is at the moment too complicated for me to modify. I ask if there 
> is an "easy" extension of the code that you kindly wrote for me to handle 
> also this possibility.
>
> Thank you for everything
> 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----------------------------------------
>
> ________________________________________
> Da: Bill Dunlap [williamwdun...@gmail.com]
> Inviato: venerdì 26 marzo 2021 18.40
> A: Stefano Sofia
> Cc: r-help mailing list
> Oggetto: Re: [R] local maxima positions in a vector with duplicated values
>
> 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://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=5a635173&h=06ff70f3&f=y&p=y
> > PLEASE do read the posting guide  
> > https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=5a635173&h=e12f63e8&f=y&p=y
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
>
> Questo messaggio  stato analizzato con Libraesva ESG ed  risultato non 
> infetto.
>
>
> ________________________________
>
> 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.
>

--

Questo messaggio  stato analizzato con Libraesva ESG ed  risultato non infetto.


________________________________

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.

______________________________________________
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.

Reply via email to