Identifying positions of the last TRUEs in a sequence of TRUEs and FALSEs
Taking advantage of diff
with an appended FALSE
to catch the implied TRUE
-to-FALSE
at the end.
diff(c(x,FALSE)) == -1
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#[13] FALSE FALSE TRUE
We may look where x
is greater than shifted x
with 0
appended.
x>c(x[-1],0)
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE