Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing boolean operation on vector using grouping from second vector

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 image 318
user2188457 Avatar asked Sep 07 '26 03:09

user2188457


1 Answers

Like this:

ave(itself_broken, cumsum(startpoint), FUN = any)
like image 148
flodel Avatar answered Sep 10 '26 22:09

flodel