[R] find position

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 10 12:49:27 CEST 2007


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.
>



More information about the R-help mailing list