title: Selecting the first nth rows by group with number of rows varied
body: <p>I like to select the first (2,3,0,4) rows of each group in a data frame.</p>
  
  <pre><code>&gt; f&lt;-data.frame(group=c(1,1,1,2,2,3,4),y=c(1:7))
  &gt; 
  &gt;   group y
  &gt;      1 1
  &gt;      1 2
  &gt;      1 3
  &gt;      2 4
  &gt;      2 5
  &gt;      3 6
  &gt;      4 7
  </code></pre>
  
  <p>and obtain a data frame as follows</p>
  
  <pre><code>group y
  1 1
  1 2
  2 4
  2 5
  4 7
  </code></pre>
  
  <p>I tried to use <code>by</code> and <code>head</code> but head does not take a vector.</p>
  
  <p>Thank you for your help.</p>