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
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:
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 -
Hello
Is there a function to extract the position (i.e. row number or more
desirable the value from another column with the same rownumber ) of
last number in a vector? This is done in a loop with 1000s of columns.
e.g.
vector/column.n: na,na,10,na,2,na,na,1,na,na
fixed
Is this what you want?
x
[1] NA NA 10 NA 2 NA NA 1 NA NA
y
[1] 10 20 30 40 50 60 70 80 85 100
y[max(which(!is.na(x)))]
[1] 80
Andy
From: Anders Bjørgesæter
Hello
Is there a function to extract the position (i.e. row number or more
desirable the value from another column
Subject
[R] find position to last number in
a vector
05/26/2006 01:30
PM