title: match certain value of one vector with range of values of other vector
body: <p>I have two vectors (same length) that only contain 0 and 1 (for simplification I used v1 and v2 in this example). I would like to count every time both v1 and v2 have value 1 at the same position OR at at a certain range for v2. For example for a range +-3 (rad=3), if the value of v1[10] is 1, I would like to know if one of the following values of v2 is also 1: v2[7], v2[8], v2[9], v2[10], v2[11], v2[12] or v[13]. The script I have now works, but it also counts the matches when the value is 0. How can I change it so it only takes into account the matches where the value is 1?</p>
  
  <pre><code>set.seed(1)
  v1 &lt;- sample(0:1, 20, replace = TRUE)
  v2 &lt;- sample(0:1, 20, replace = TRUE)
  
  matches &lt;- vector()
  rad &lt;- 3 
  
  for (i in 1:length(v1)){
  
    if ((i - rad) &lt; 0){
  
      matches[i] &lt;- ifelse(v1[i] %in% v2[1:rad], TRUE, FALSE)
  
    } else{
  
      matches[i] &lt;- ifelse(v1[i] %in% v2[(i-rad):(i+rad)], TRUE, FALSE)
  
   }
  
  }
  </code></pre>