Jason Liao
2003-Mar-05 21:36 UTC
[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
Marc Schwartz
2003-Mar-05 22:03 UTC
[R] how to find the location of the first TRUE of a logical vector
>-----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_liaoIf '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
Douglas Bates
2003-Mar-05 22:09 UTC
[R] how to find the location of the first TRUE of a logical vector
Jason Liao <jg_liao at yahoo.com> writes:> without having to check the vector element by element? Thanks a lot!If vec is your vector of logical values then match(TRUE, vec) should work. It will return NA if there are no TRUE values.
Martina Pavlicova
2003-Mar-05 22:14 UTC
[R] how to find the location of the first TRUE of a logical vector
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 >
Roger Peng
2003-Mar-05 22:19 UTC
[R] how to find the location of the first TRUE of a logical vector
Try, x <- c(F,F,F,T,F,T,F,T) min(which(x)) -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng 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 >
J.R. Lockwood
2003-Mar-05 22:41 UTC
[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!which.max() will coerce the logical to numeric and give the location of the first max, which is the first TRUE. J.R. Lockwood 412-683-2300 x4941 lockwood at rand.org http://www.rand.org/methodology/stat/members/lockwood/
Jason Liao
2003-Mar-06 02:42 UTC
[R] how to find the location of the first TRUE of a logical vector
Many thanks to all who replied: Marc Schwartz, Douglas Grove, Spencer Graves, J.R. Lockwood, Roger Peng, Martina Pavlicova, Steve Su,james.holtman, Wiener, Matthew, Domijan, Katarina and Douglas Bates. Let x be the logical vector. The best solution seems to be: which(x)[1]. Now my code is much simpler. 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
Jason Liao
2003-Mar-06 02:42 UTC
[R] how to find the location of the first TRUE of a logical vector
Many thanks to all who replied: Marc Schwartz, Douglas Grove, Spencer Graves, J.R. Lockwood, Roger Peng, Martina Pavlicova, Steve Su,james.holtman, Wiener, Matthew, Domijan, Katarina and Douglas Bates. Let x be the logical vector. The best solution seems to be: which(x)[1]. Now my code is much simpler. 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
Jason Liao
2003-Mar-06 02:43 UTC
[R] how to find the location of the first TRUE of a logical vector
Many thanks to all who replied: Marc Schwartz, Douglas Grove, Spencer Graves, J.R. Lockwood, Roger Peng, Martina Pavlicova, Steve Su,james.holtman, Wiener, Matthew, Domijan, Katarina and Douglas Bates. Let x be the logical vector. The best solution seems to be: which(x)[1]. Now my code is much simpler. 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