title: Group and compare numbers in a list
body: <p>Consider the following data frame obtained after a <code>cbind</code> operation on two lists </p>
  
  <pre><code>&gt; fl
    x meanlist
  1 1     48.5
  2 2     32.5
  3 3     28.0
  4 4     27.0
  5 5     25.5
  6 6     20.5
  7 7     27.0
  8 8     24.0
  
  class_median &lt;- list(0, 15, 25, 35, 45)
  class_list &lt;- list(0:10, 10:20, 20:30, 30:40, 40:50)
  </code></pre>
  
  <p>The values in <code>class_median</code> represent classes -10 to +10, 10 to 20, 20 to 30 etc</p>
  
  <p>Firstly, I am trying to group the values in <code>fl$meanlist</code> as per the classes in <code>class_list</code>. Secondly, I am trying to return one value per class which is closest to the median values as follows</p>
  
  <pre><code>&gt; fl_subset
    x meanlist cm
  1 1     48.5 45
  2 2     32.5 35
  3 5     25.5 25
  </code></pre>
  
  <p>I am trying to use loops to compare but it seems to be long and unmanageable and the result is not correct  </p>