Hi, all. I'd like to know if exists a manner to get the first index where a condition is attained in a vector. For example, There is a better solution than first.index <- table(subject[corretor==27])[1] (give me the subject for the first time that corretor is 27)? Thanks, =======================================Cezar Freitas (ICQ 109128967) IMECC - UNICAMP Campinas, SP - Brasil
Have you considered "which", as in the following: > a <- rep(1:2, 2) > a [1] 1 2 1 2 > which(a==1) [1] 1 3 > which(a==1)[1] [1] 1 hope this helps. spencer graves Cezar Augusto de Freitas Anselmo wrote:>Hi, all. I'd like to know if exists a manner to get the first index where >a condition is attained in a vector. For example, > >There is a better solution than > >first.index <- table(subject[corretor==27])[1] > >(give me the subject for the first time that corretor is 27)? > >Thanks, > >=======================================>Cezar Freitas (ICQ 109128967) >IMECC - UNICAMP >Campinas, SP - Brasil > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
((1:length(corretor))[corretor==27])[1] should also work, and avoids computing table. Giovanni Petris> Date: Fri, 19 Sep 2003 15:15:50 -0300 (BRT) > From: Cezar Augusto de Freitas Anselmo <cafa at ime.unicamp.br> > Sender: r-help-bounces at stat.math.ethz.ch > Precedence: list > > Hi, all. I'd like to know if exists a manner to get the first index where > a condition is attained in a vector. For example, > > There is a better solution than > > first.index <- table(subject[corretor==27])[1] > > (give me the subject for the first time that corretor is 27)? > > Thanks, > > =======================================> Cezar Freitas (ICQ 109128967) > IMECC - UNICAMP > Campinas, SP - Brasil > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
On Fri, 2003-09-19 at 13:15, Cezar Augusto de Freitas Anselmo wrote:> Hi, all. I'd like to know if exists a manner to get the first index where > a condition is attained in a vector. For example, > > There is a better solution than > > first.index <- table(subject[corretor==27])[1] > > (give me the subject for the first time that corretor is 27)? > > Thanks,first.index <- min(which(corretor == 27)) For example: corretor <- c(15, 23, 27, 34, 25, 27, 26) which(corretor == 27) [1] 3 6 min(which(corretor == 27)) [1] 3 See ?which HTH, Marc Schwartz