# [R] how to find the location of the first TRUE of a logical vector

Giles Innocent gti1x at vet.gla.ac.uk
Thu Mar 6 10:46:57 CET 2003

```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: 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
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

```

More information about the R-help mailing list