[R] which.minimums not which.min
Philippe Grosjean
phgrosjean at sciviews.org
Wed Mar 15 21:45:28 CET 2006
What Fred is looking for is local minima/maxima, also known as turning
points, or pits/peaks in a series. You can look at ?turnpoints in
pastecs library.
> x <- c(1:4,0:5, 4, 11)
> x
[1] 1 2 3 4 0 1 2 3 4 5 4 11
> tp <- turnpoints(x)
> summary(tp)
Turning points for: x
nbr observations : 12
nbr ex-aequos : 0
nbr turning points: 4 (first point is a peak)
E(p) = 6.666667 Var(p) = 1.811111 (theoretical)
point type proba info
1 4 peak 0.100000000 3.3219281
2 5 pit 0.002380952 8.7142455
3 10 peak 0.005952381 7.3923174
4 11 pit 0.666666667 0.5849625
> plot(tp) # Only useful for a longer and more complex series!
> # Get the position of peaks
> (1:length(x))[extract(tp, no.tp = FALSE, peak = TRUE, pit = FALSE)]
[1] 4 10
Warning message:
arguments after the first two are ignored in: UseMethod("extract", e, n,
...)
> (1:length(x))[extract(tp, no.tp = FALSE, peak = FALSE, pit = TRUE)]
[1] 5 11
Warning message:
arguments after the first two are ignored in: UseMethod("extract", e, n,
...)
> # By the way, there are warnings although it works well (I ask on R-Help)
Now, you can easily code your which.minima() function using turnpoints:
x <- c(1:4,0:5, 4, 11)
x
tp <- turnpoints(x)
summary(tp)
plot(tp) # Only useful for a longer and more complex series!
# Get the position of peaks
(1:length(x))[extract(tp, no.tp = FALSE, peak = TRUE, pit = FALSE)]
(1:length(x))[extract(tp, no.tp = FALSE, peak = FALSE, pit = TRUE)]
# By the way, there are warnings although it works well (I ask on R-Help)
which.minima <- function(x) {
if (!require(pastecs)) stop("pastecs library is required!")
x <- as.vector(x)
(1:length(x))[extract(turnpoints(x), no.tp = FALSE, peak = FALSE, pit =
TRUE)]
}
which.minima(x)
Of course, you could optimize this code. This is just a rough solution!
Best,
Philippe Grosjean
..............................................<°}))><........
) ) ) ) )
( ( ( ( ( Prof. Philippe Grosjean
) ) ) ) )
( ( ( ( ( Numerical Ecology of Aquatic Systems
) ) ) ) ) Mons-Hainaut University, Pentagone (3D08)
( ( ( ( ( Academie Universitaire Wallonie-Bruxelles
) ) ) ) ) 8, av du Champ de Mars, 7000 Mons, Belgium
( ( ( ( (
) ) ) ) ) phone: + 32.65.37.34.97, fax: + 32.65.37.30.54
( ( ( ( ( email: Philippe.Grosjean at umh.ac.be
) ) ) ) )
( ( ( ( ( web: http://www.umh.ac.be/~econum
) ) ) ) ) http://www.sciviews.org
( ( ( ( (
..............................................................
Marc Schwartz (via MN) wrote:
> On Wed, 2006-03-15 at 11:32 -0800, Fred J. wrote:
>
>> Hi
>>
>> Is there a function which determines the location, i.e., index of
>>the all minimums or maximums of a numeric vector.
>> Which.min(x) only finds the (first) of such.
>>
>> > x <- c(1:4,0:5, 4, 11)
>> > x
>> [1] 1 2 3 4 0 1 2 3 4 5 4 11
>> > which.min(x)
>> [1] 5
>> > which.max(x)
>> [1] 11
>> >
>>
>> but I need
>> which.min(x) to be 5 11
>> which.max(x) to be 4 10
>>
>> thanks
>>
>
>
> There is something wrong with your example code versus data here, since:
>
>
>>x
>
> [1] 1 2 3 4 0 1 2 3 4 5 4 11
>
>
>>which.min(x)
>
> [1] 5
>
>
>>which.max(x)
>
> [1] 12
>
>
> There is one one minimum value of 0 in that vector and only one maximum
> value of 11.
>
> If you had a vector 'x':
>
>
>>x <- c(1:4, 0:5, 4, 0, 5)
>
>
>>x
>
> [1] 1 2 3 4 0 1 2 3 4 5 4 0 5
>
>
> You could then do the following to get the indices of the multiple
> min/max values:
>
>
>>which(x == min(x))
>
> [1] 5 12
>
>
>>which(x == max(x))
>
> [1] 10 13
>
>
> The only other thing that I can think you might be considering would be
> local minima/maxima in the vector and if that is what you want using:
>
> RSiteSearch("local minima")
>
> or
>
> RSiteSearch("peaks")
>
>
> should lead you to some solutions that have been discussed previously.
>
> HTH,
>
> Marc Schwartz
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list