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]]
which(a == .4)[1] b On Jun 10, 2007, at 4:45 AM, gallon li 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.
try this:
which(a == 0.4)[1]
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
Quoting gallon li <gallon.li at gmail.com>:
> 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.
>
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
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. >