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 __________________________________________________ [[alternative HTML version deleted]]
Try order(x, decreasing=TRUE/FALSE) -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Fred J. Sent: Wednesday, March 15, 2006 2:32 PM To: r-help at stat.math.ethz.ch Subject: [R] which.minimums not which.min 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 __________________________________________________ [[alternative HTML version deleted]] ______________________________________________ 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
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
What you want seems to be the valleys and peaks in the data. If so, try: RSiteSearch("find peaks") which points to a post by Philippe Grosjean, pointing to the pastesc package:> library(pastecs)Loading required package: boot> tp <- turnpoints(x) > which(tp$peaks)[1] 4 10> which(tp$pits)[1] 5 11 Andy From: Fred J.> > 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 > > > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > >