[R] Locate first index

Marc Schwartz MSchwartz at medanalytics.com
Fri Sep 19 20:40:48 CEST 2003


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




More information about the R-help mailing list