Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return last match from vector

Tags:

r

Is there a simple way to get the index of the last match of a vector?

lastInflectionRow(c(TRUE,FALSE,TRUE,FALSE,FALSE))

lastInflectionRow<-function(temp){
    m<-match(TRUE,temp,nomatch=NA)
    m
}

GOAL: 3

like image 304
Rilcon42 Avatar asked May 24 '16 03:05

Rilcon42


1 Answers

Another simple way could be using max on the index of TRUE elements.

x <- c(TRUE,FALSE,TRUE,FALSE,FALSE)

max(which(x))

#[1] 3
like image 100
Ronak Shah Avatar answered Oct 12 '22 08:10

Ronak Shah