I have two vectors with binary values that represent information about some data vector. The first vector indentifies whether a certain element of the data vector is broken. The second vector identifies the extend to which other elements are affected and hence also broken. The vectors look like this.
itself_broken = c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE)
startpoint = c(TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE)
I now want to find all elements that are broken in the following sense: If one element between two startpoints is broken, all others between these two startpoints (including the left startpoint) are too. So in the above example the resulting vector should be:
all_broken = c(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE)
I could implement this by using a loop for every itself_broken element going upwards, marking elements as broken until hitting a startpoint. But this seems really inefficient to me.
What is the right way to solve this?
Like this:
ave(itself_broken, cumsum(startpoint), FUN = any)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With