local maxima positions in a vector with duplicated values
WARNING: I have not carefully tested the following, so you will need to do so before using. Like Bill, I found rle a clearer approach. Here's my (not so elegant, probably) version:
x <-c(1,0,0,0,2,2,3,4,0,1,1,0,5,5,5,0,1) z <- rle(x) vals <- z$values n <- length(vals) whmin<-c(vals[-1], -Inf) > vals & c(-Inf, vals[-n]) > vals whmax <- c(vals[-1], Inf) < vals & c(Inf, vals[-n]) < vals cumsum(z$lengths)[whmin]
[1] 4 9 12 16
cumsum(z$lengths)[whmax[-1]] +1
[1] 8 10 13 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Mar 26, 2021 at 8:36 AM Stefano Sofia <
stefano.sofia at 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.sofia at 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 at 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.