[R] how to find the location of the first TRUE of a logical vector
Marc Schwartz
mschwartz at medanalytics.com
Wed Mar 5 23:03:49 CET 2003
>-----Original Message-----
>From: r-help-bounces at stat.math.ethz.ch
>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jason Liao
>Sent: Wednesday, March 05, 2003 3:36 PM
>To: r-help at stat.math.ethz.ch
>Subject: [R] how to find the location of the first TRUE of a
>logical vector
>
>
>without having to check the vector element by element? Thanks a lot!
>
>Jason
>
>=====
>Jason G. Liao, Ph.D.
>Division of Biometrics
>University of Medicine and Dentistry of New Jersey
>335 George Street, Suite 2200
>New Brunswick, NJ 08903-2688
>phone (732) 235-8611, fax (732) 235-9777
>http://www.geocities.com/jg_liao
If 'lv' if your logical vector, you could use something like:
min(which(lv == TRUE))
which() would return a vector of the indices within 'lv' that match
TRUE and of course min() will give you the lowest index value.
An example:
> lv <- c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE)
> lv
[1] FALSE FALSE FALSE TRUE TRUE FALSE TRUE
> min(which(lv == TRUE))
[1] 4
HTH,
Marc Schwartz
More information about the R-help
mailing list