title: Compare two vectors of numbers based on threshold of tolerance (±) of 0.5
body: <p>I have two vectors <code>g</code> and <code>h</code>. I want to compare the numbers in these two vectors and find out whether there are any common elements between them. But the common elements do not have to be exactly the same and can be within a range of <code>(-0.5, +0.5)</code>. Therefore, <code>g±0.5</code> is being compared with <code>h±0.5</code>.</p>
  
  <pre><code>g &lt;- c(0.5, 5956.3, 38, 22.666, 590.3, 21.992, 9.3)
  h &lt;- c(0.7, 99.2, 39, 30, 21.68, 9.4, 22.333, 0.001, 0.000222, 9.999)
  </code></pre>
  
  <p>As an example, in the two vectors above, <code>0.5</code> from <code>g</code> and <code>0.7</code> from <code>h</code> match because they are in the vicinity of <code>±0.5</code> from each other. <code>9.4</code> and <code>9.3</code> also match. And furthermore, <code>22.666</code> and <code>22.333</code> also match, because their difference is also in the range <code>(-0.5, +0.5)</code>.</p>
  
  <p>It is important to note that <strong>EVERY element</strong> of <code>g</code> should be compared to <strong>EVERY element</strong> of <code>h</code>.</p>
  
  <p>Is there a function to do this in <code>R</code>?</p>
  
  <p><code>all.equal</code> function unfortunately only compares each element from one vector to the element with the same index from another vector and thus expects equal length from the vectors. What I want to do is that I want to compare each element of vector <code>g</code> with each element of vector <code>h</code>.</p>