title: Subset panel data by group
body: <p>I would like to subset an unbalanced panel data set by group. For each group, I would like to keep the two observations in the first and the last years. </p>
  
  <p>How do I best do this in R? For example:</p>
  
  <pre><code>dt &lt;- data.frame(name= rep(c("A", "B", "C"), c(3,2,3)), 
                   year=c(2001:2003,2000,2002,2000:2001,2003))
  
  &gt; dt
    name year
  1    A 2001
  2    A 2002
  3    A 2003
  4    B 2000
  5    B 2002
  6    C 2000
  7    C 2001
  8    C 2003
  </code></pre>
  
  <p>What I would like to have:</p>
  
  <pre><code>  name year
  1    A 2001
  3    A 2003
  4    B 2000
  5    B 2002
  6    C 2000
  8    C 2003
  </code></pre>