Or even:

which(lv)[1]

as lv is already a logical vector

Giles

On 2003.03.05 22:44 Spencer Graves wrote:
Also:

which(lv == TRUE)[1]

Spencer Graves

Marc Schwartz wrote:
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jason Liao
Sent: Wednesday, March 05, 2003 3:36 PM
To: [EMAIL PROTECTED]
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

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help


______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to