dplyr
provides the cumany
and cumall
functions, which perform element-wise &&
and ||
operations on vectors.
cumall
traverses the vector and returns TRUE
for each element, until it encounters an element that does not meet the specified criteria. All remaining elements in its output will be FALSE
.
Here, cumall
searches the vector for "G", and returns TRUE
in the first 3 positions, because they are all "G". But after it hits "A" in the fourth position, it returns FALSE
for each remaining element.
x <- c("G","G","G","A","G","T","T","C")
cumall(x == "G") # Same as x[1]=="G", x[1]=="G" && x[2]=="G", etc.
## [1] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
You can think of the cumall
function as answering the question "Have all the elements in the vector up to this point met the criteria?"