title: Weighted average of pair elements in a vector in R
body: <p>I have two vectors <code>x</code> and <code>w</code>. vector <code>w</code> is a numerical vector of weights the same length as x.
  How can we get the  weighted average of the fisrt pair elements in vector <code>x</code> which their difference are small (for example tol= 1e-2) and then in the next iteration do the same thing for the next pair until there is no pair which their difference is less than tol? For example, these vectors are as follows:</p>
  
  <pre><code>     x = c(0.0001560653, 0.0001591889, 0.0001599698, 0.0001607507, 0.0001623125,
             0.0001685597, 0.0002793819, 0.0006336307, 0.0092017241, 0.0092079042,
             0.0266525118, 0.0266889564, 0.0454923285, 0.0455676525, 0.0457005450)
       w = c(2.886814e+03, 1.565955e+04, 9.255762e-02, 7.353589e+02, 1.568933e+03,
             5.108046e+05, 6.942338e+05, 4.912165e+04, 9.257674e+00, 3.609918e+02,
             8.090436e-01, 1.072975e+00, 1.359145e+00, 9.828314e+00, 9.455688e+01)
  </code></pre>
  
  <p>I want to find which pair elements of <code>x</code> has the minimum differences and after finding this pair , get the weighted average mean. I try this code but this one does not give me the result. How can i find the index of <code>min(diff(x))</code> and check that is it less than tol or not?</p>
  
  <pre><code>        &gt; min(diff(x))
          &gt; which(min(diff(x)) &lt; 1e-2)
  </code></pre>