title: How to find the latest data by variable
body: <p>I would like to know how to extract data by latest date by coding.</p>
  
  <pre><code>cname       year   x1 x2 x3 x4
  Afghanistan 2015   3  2  6  3
  Afghanistan 2016   4  7  NA 9
  Afghanistan 2017   5  NA NA NA  
  Albania     2015   2  3  4  3
  Albania     2016   2  4  NA NA
  Albania     2017   4  NA 8  NA  
  Algeria     2015   NA NA NA NA
  Algeria     2016   NA NA NA NA
  Algeria     2017   NA NA NA NA
  ...
  </code></pre>
  
  <p>The answer I would like to get</p>
  
  <pre><code> cname          x1 x2 x3 x4
     Afghanistan   5  7  6  9
     Albania       4  4  8  3
     Algeria      NA NA NA NA
  ...
  </code></pre>
  
  <p>I thied to find the answers here, but what I've found is only subsetting the last observation by grouping.
   Thanks for your help!</p>