title: R-ranking values of a column by grouping, conditional to another variable
body: <p>I have such a data frame(df):</p>
  
  <pre><code>group col1 col2  
  x      a    22    
  x      a    23  
  x      b    16  
  x      b    18  
  y      a    11  
  y      a    12  
  y      a    16  
  y      a    45  
  y      b    24  
  </code></pre>
  
  <p>Desired output is:</p>
  
  <pre><code>group col1 col2 rank 
  x      a    22  1  
  x      a    23  2
  x      b    16  0
  x      b    18  0
  y      a    11  1
  y      a    12  2
  y      a    16  3
  y      a    45  4
  y      b    24  0
  </code></pre>
  
  <p>Namely,</p>
  
  <ul>
  <li>order col2 by group and col1</li>
  <li>when col1="b" then rank is 0</li>
  <li>rank values of col2 from smallest to largest</li>
  </ul>
  
  <p>How can I do that by using R? I will be very glad for any help. Thanks a lot. </p>