[R] which.minimums not which.min
Marc Schwartz (via MN)
mschwartz at mn.rr.com
Wed Mar 15 21:14:17 CET 2006
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
More information about the R-help
mailing list