Re: [R] find position

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun, 10 Jun 2007 06:49:27 -0400

Try

match(0.4, a)

Also see ?match and the nomatch= argument, in particular. If your numbers are only equal to within an absolute tolerance, tol, as discussed in the R FAQ

   http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f you may need:

   tol <- 1e-6
   match(TRUE, abs(a-0.4) < tol)

or

   which(abs(a-0.4) < tol)[1] # tol from above

and analogously if a relative tolerance is required.

On 6/10/07, gallon li <gallon.li_at_gmail.com> wrote:
> find the position of the first value who equals certain number in a vector:
>
> Say a=c(0,0,0,0,0.2, 0.2, 0.4,0.4,0.5)
>
> i wish to return the index value in a for which the value in the vector is
> equal to 0.4 for the first time. in this case, it is 7.
>
> [[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
> and provide commented, minimal, self-contained, reproducible code.
>



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 and provide commented, minimal, self-contained, reproducible code. Received on Sun 10 Jun 2007 - 10:53:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 10 Jun 2007 - 15:31:51 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.