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

Martina Pavlicova pavlicov at stat.ohio-state.edu
Wed Mar 5 23:14:10 CET 2003


It might not be the most elegant, but it works:

> foo <- c(F, F, T, F, T, T, F ,T)
> c(1:length(foo))[foo][1]
[1] 3
>

And if there is no 'T', it returns 'NA'

> foo <- c(F, F)
> c(1:length(foo))[foo][1]
[1] NA

Martina

--------------------------------------------------------------------------
Department of Statistics             Office Phone: (614) 292-1567
1958 Neil Avenue, 304E Cockins Hall  FAX: (614) 292-2096
The Ohio State University            E-mail: pavlicov at stat.ohio-state.edu
Columbus, OH 43210-1247              www.stat.ohio-state.edu/~pavlicov


On Wed, 5 Mar 2003, Jason Liao wrote:

> 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
>
> ______________________________________________
> 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