有没有一种简单的方法来获取向量的最后一个匹配的索引?
lastInflectionRow(c(TRUE,FALSE,TRUE,FALSE,FALSE))
lastInflectionRow<-function(temp){
m<-match(TRUE,temp,nomatch=NA)
m
}
Run Code Online (Sandbox Code Playgroud)
目标:3
另一种简单的方法是使用元素max索引TRUE.
x <- c(TRUE,FALSE,TRUE,FALSE,FALSE)
max(which(x))
#[1] 3
Run Code Online (Sandbox Code Playgroud)
?Position在使用right=TRUE参数时,就是为了这种事情.以下所有内容应基本相同.
Position(I, x, right=TRUE)
#[1] 3
Position(identity, x, right=TRUE)
#[1] 3
Position(isTRUE, x, right=TRUE)
#[1] 3
Position(function(x) x, x, right=TRUE)
#[1] 3
Run Code Online (Sandbox Code Playgroud)