[R] how to find the location of the first TRUE of a logical vector
mschwartz at medanalytics.com
Wed Mar 5 23:03:49 CET 2003
>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
>without having to check the vector element by element? Thanks a lot!
>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
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.
> lv <- c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE)
 FALSE FALSE FALSE TRUE TRUE FALSE TRUE
> min(which(lv == TRUE))
More information about the R-help