title: Conditional setdiff (all-to-all) on two columns from two dataframes with a numeric range for taking a match
body: <p>Here are two example data frames:</p>
  
  <pre><code>df1 &lt;- data.frame(Time1v1 = c(55.25, 59.36, 40.26, 786.008, 980.569, 11.2, 10.11, 23.11),
                    Time2v1 = c(81, 12, 13, 11.0112, 93.9, 14.8, 15.3, 78.91))
  
  df2 &lt;- data.frame(Time1v2 = c(10.13, 980.659, 14.42, 90.1, 40.3298, 9234, 59.35),
                    Time2v2 = c(25.1, 88.9, 120, 911, 22.1253, 81, 15.1))
  
  
  &gt; df1
    Time1v1 Time2v1
  1  55.250 81.0000
  2  59.360 12.0000
  3  40.260 13.0000
  4 786.008 11.0112
  5 980.569 93.9000
  6  11.200 14.8000
  7  10.110 15.3000
  8  23.110 78.9100
  
  &gt; df2
      Time1v2 Time2v2
  1   10.1300   25.1000
  2  980.6590   88.9000
  3   14.4200  120.0000
  4   90.1000  911.0000
  5   40.3298   22.1253
  6 9234.0000   81.0000
  7   59.3500   15.1000
  </code></pre>
  
  <p>I want to compare each and every row of <code>df1</code> with each and every row of <code>df2</code>. If the difference between <code>Time1</code> from df1 and df2 is in the range <code>[-0.1,+0.1]</code> AND difference in <code>Time2</code> is in the range <code>[-10,+10]</code> then that particular row from df1 must be removed.</p>
  
  <hr>
  
  <p><strong>ATTEMPT TO SOLVE</strong></p>
  
  <p>Here's an attempt to <strong>solve</strong> this. Is there a better way?</p>
  
  <pre><code>df1$remove &lt;- rep("No", nrow(df1))
  for(i in 1:nrow(df1)){
      for(j in 1:nrow(df2)){
          if(data.table::inrange(df1$Time1v1[i], df2$Time1v2[j] - 0.1, df2$Time1v2[j] + 0.1) &amp;&amp; data.table::inrange(df1$Time2v1[i], df2$Time2v2[j] - 10, df2$Time2v2[j] + 10)) {df1$remove[i] &lt;- "remove"}
      }
  }
  </code></pre>
  
  <p>This gives me:</p>
  
  <pre><code>&gt; df1
        Time1v1 Time2v1 remove
      1  55.250 81.0000     No
      2  59.360 12.0000 remove
      3  40.260 13.0000 remove
      4 786.008 11.0112     No
      5 980.569 93.9000 remove
      6  11.200 14.8000     No
      7  10.110 15.3000 remove
      8  23.110 78.9100     No
  </code></pre>
  
  <hr>
  
  <p><strong>EXPECTED FINAL RESULT</strong></p>
  
  <p>And <strong>finally</strong> the expected <strong>output</strong> will be:</p>
  
  <pre><code>&gt; df1[which(df1$remove != "remove"),-3]
  
    Time1v1 Time2v1
  1  55.250 81.0000
  4 786.008 11.0112
  6  11.200 14.8000
  8  23.110 78.9100
  </code></pre>
  
  <hr>
  
  <p><strong>RELATED</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/50043291/perform-non-pairwise-all-to-all-comparisons-between-two-unordered-character-vect">Perform non-pairwise all-to-all comparisons between two unordered character vectors --- The opposite of intersect --- all-to-all setdiff</a></p>
  
  <p><a href="https://stackoverflow.com/questions/51326872/all-to-all-setdiff-on-two-numeric-vectors-with-a-numeric-threshold-for-accepting?noredirect=1&amp;lq=1">All-to-all setdiff on two numeric vectors with a numeric threshold for accepting matches</a></p>