A more general version of this question has been answered here. A user proposed I ask this more specific version of the question as a separate post.
I have two logical vectors which look like this:
x = c(0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0)
y = c(0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0)
I want to count the intersections between ranges of consecutive values (in this example 1111) in such a way, that at most one intersection per run of 1s in the first vector is counted.
Using sum(rle(x & y)$values)
from the above mentioned answer, I can count the total number of intersections of the above vectors as two, but I expect one.
Do you want max length of the intersection to be 1. If so, you can do
sum(with(rle(x & y), lengths == 1 & values))
#[1] 1
When we do x & y
the numeric values of x
and y
are changed to logical values where 1 is represented as TRUE
and FALSE
as 0. Since we are only interested in intersection of 1's (i.e TRUE
) with at most 1 intersection we count the number of times where this condition is satisfied i.e lengths == 1
(or lengths <= 1
as we need to check for at most) and values
meaning only 1's i.e TRUE
.