title: Ranking within each group
body: <p>Consider the data:</p>
  
  <pre><code>set.seed(123)
  
  x &lt;- rbinom(12, 1, .5)  
  y &lt;- (x==0) * rexp(12, 1/100)
  z &lt;- (x==1) * rexp(12, 1/220)
  group &lt;- sample( rep(1:2, each=6) )
  
  d &lt;- data.frame(x, y, z, group)
  </code></pre>
  
  <p>Sorting the data first by <code>y</code>, then by <code>z</code></p>
  
  <pre><code>d &lt;- d[order(d$y,d$z),]
  </code></pre>
  
  <p>Now within each group, I want to give rank . The following codes work correctly:</p>
  
  <pre><code>ds &lt;- split(d, d$group)
  
  ds1 &lt;- ds[[1]]
  ds1$rank &lt;- 1:nrow(ds1)
  
  ds2 &lt;- ds[[2]]
  ds2$rank &lt;- 1:nrow(ds2)
  </code></pre>
  
  <p>But without splitting the data frame, I want to rank within each group. How can I do that?</p>