marzo 2021 18.03
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
Your original query included
> x <- c(1,0,0,0,2,2,3,4,0,1,1,0,5,5,5,0,1)
... I need 8 10 13.
Did you also wa
@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
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
no 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 m
Stefano,
i notice that my and Bill's (pretty much isomorphic) solutions give a
zero-length vector. i don't remember Bert's, but it's probably gives
similar results. your code could check for that result, and react
accordingly.
cheers, Greg
__
R-help@
.@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 val
Stefano,
my contribution is similar to Bill Dunlap's:
x <- c(1,0,0,0,2,2,3,4,0,1,1,0,5,5,5,0,1)
(cumsum(rle(x)$lengths)-(rle(x)$length-1))[which(diff(diff(rle(x)$values)>=0)<0)+1]
cumsum(rle(x)$lengths)[which(diff(diff(rle(x)$value)>0)>0)+1]
kind of a cute little problem, actually.
che
Hi Stefano,
My package, vectools, is partly designed for this purpose.
(Unfortunately, the package *is* subject to *change*, and some of the
functions may change in the next update).
library (vectools)
which.maxs (x, ret.type="intervals")[,1] # c (8, 10, 13)
which.mins (x, ret.type="intervals")[,
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],
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,FAL
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
11 matches
Mail list logo