title: adding a column to a data frame in R based on the rank of another column
body: <p>Here is a reproducible example of my data. For the following data frame:</p>
  
  <pre><code>df &lt;- data.frame(Subject = c('John', 'John', 'John', 'John','Mary', 'Mary', 'Mary', 'Mary'),
                   SNR = c(-4,-4,0,4,0,4,4,8))
  </code></pre>
  
  <p>I would like to add a column 'rank' that provides a ranking for SNR by Subject, so that it would look like this:</p>
  
  <pre><code>Subject   SNR   Rank
  John      -4    1
  John      -4    1
  John       0    2
  John       4    3
  Mary       0    1
  Mary       4    2
  Mary       4    2
  Mary       8    3
  </code></pre>
  
  <p>I have tried using:</p>
  
  <pre><code>dfNew &lt;- transform(df, Rank = ave(SNR, Subject, FUN = function(x) rank(x, ties.method = "first")))
  </code></pre>
  
  <p>But I get the following:</p>
  
  <pre><code>Subject   SNR   Rank
  John      -4    1
  John      -4    2
  John       0    3
  John       4    4
  Mary       0    1
  Mary       4    2
  Mary       4    3
  Mary       8    4   
  </code></pre>
  
  <p>I have also tried using the different ties.method options, but none give me what I am looking for (i.e., ranking only from 1-3).</p>
  
  <p>Any help would be much appreciated! </p>