Is there a function in R, which would return index of maximum value in a vector ? e.g.> v <- round(10*rnorm(8)) > v[1] 6 -3 -6 15 7 9 0 -19> max(v)[1] 15 ??? index.max(v) ??? 4
?which.max -roger ryszard.czerminski at pharma.novartis.com wrote:> Is there a function in R, which would return index of maximum value > in a vector ? > > e.g. > > >>v <- round(10*rnorm(8)) >>v > > [1] 6 -3 -6 15 7 9 0 -19 > >>max(v) > > [1] 15 > > ??? index.max(v) > ??? 4 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
which(v==max(v)) should do the trick. HTH, Jim James W. MacDonald Affymetrix and cDNA Microarray Core University of Michigan Cancer Center 1500 E. Medical Center Drive 7410 CCGC Ann Arbor MI 48109 734-647-5623>>> <ryszard.czerminski at pharma.novartis.com> 11/14/03 02:05PM >>>Is there a function in R, which would return index of maximum value in a vector ? e.g.> v <- round(10*rnorm(8)) > v[1] 6 -3 -6 15 7 9 0 -19> max(v)[1] 15 ??? index.max(v) ??? 4 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
The which function may do what you want: which(v1==max(v1))> v1<-round(10*rnorm(8)) > v1[1] 7 17 0 -11 1 6 -10 0> which(v1==max(v1))[1] 2> max(v1)[1] 17> v2<-c(v1,max(v1)) > which(v2==max(v2))[1] 2 9 You can always create logicals but what do you want returned if the max happens more than once? bob -----Original Message----- From: ryszard.czerminski@pharma.novartis.com [mailto:ryszard.czerminski@pharma.novartis.com] Sent: Friday, November 14, 2003 2:06 PM To: R-help@stat.math.ethz.ch Subject: [R] index of max value ? Is there a function in R, which would return index of maximum value in a vector ? e.g.> v <- round(10*rnorm(8)) > v[1] 6 -3 -6 15 7 9 0 -19> max(v)[1] 15 ??? index.max(v) ??? 4 ______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help [[alternative HTML version deleted]]
On Fri, 14 Nov 2003 ryszard.czerminski at pharma.novartis.com wrote:> Is there a function in R, which would return index of maximum value > in a vector ? >Yes. which.max(), which is mentioned in the See Also section in help(max). -thomas
?which v <- c(6, -3, -6, 15, 7, 9, 0 -19) v which(v == max(v)) At 02:05 PM 11/14/2003 -0500, ryszard.czerminski at pharma.novartis.com wrote:>Is there a function in R, which would return index of maximum value >in a vector ? > >e.g. > >> v <- round(10*rnorm(8)) >> v >[1] 6 -3 -6 15 7 9 0 -19 >> max(v) >[1] 15 > >??? index.max(v) >??? 4 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >With best wishes and kind regards I am Sincerely, Corey A. Moffet Rangeland Scientist USDA-ARS Northwest Watershed Research Center 800 Park Blvd, Plaza IV, Suite 105 Boise, ID 83712-7716 Voice: (208) 422-0718 FAX: (208) 334-1502
Have you considered: > x <- c(1, 3, 1) > which(x==max(x)) [1] 2 Is this what you want? spencer graves ryszard.czerminski at pharma.novartis.com wrote:>Is there a function in R, which would return index of maximum value >in a vector ? > >e.g. > > > >>v <- round(10*rnorm(8)) >>v >> >> >[1] 6 -3 -6 15 7 9 0 -19 > > >>max(v) >> >> >[1] 15 > >??? index.max(v) >??? 4 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >