[R] Locate first index

Douglas Bates bates at stat.wisc.edu
Fri Sep 19 22:56:37 CEST 2003


It may be easier to use the match function which is defined to return
the index of the first match.

> corretor <- c(15, 23, 27, 34, 25, 27, 26)
> match(27, corretor)
[1] 3

Marc Schwartz <MSchwartz at medanalytics.com> writes:

> 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




More information about the R-help mailing list