title: How do I group by then filter without losing rows from data in R?
body: <p>I have a sample <code>tbl_df</code> that I am trying to find a solution to.  I am trying to do the following at a high level.  Compare the max score for a student in 2021 (based on which type count they have the most of) to their most recent result in that <code>type</code> in the most recent year before 2021.  I'd like to use <code>dplyr::filter</code> but can't figure out how to <code>filter</code> properly to retain the <code>tbl_df</code> to get to my output.</p>
  <p>In brief:</p>
  <ol>
  <li>Group by <code>full_name</code> and choose the <code>type</code> row that has the <code>max</code> value in the <code>count</code> column for 2021</li>
  <li>Choose the next most recent year for that same <code>type</code></li>
  </ol>
  <p>As you can see, since Eric Collins doesn't have a row in 2020, his most recent year is 2019, while the others have values in 2020.</p>
  <p>Sample:</p>
  <pre><code>sample_df &lt;- tibble::tribble(
                    ~year,           ~full_name,        ~type, ~count, ~avg_score, ~max,
                    2021L,       &quot;Jason Valdez&quot;,   &quot;Sciences&quot;,   &quot;33&quot;,         98,   99,
                    2021L,       &quot;Jason Valdez&quot;, &quot;Humanities&quot;,   &quot;59&quot;,         97,   99,
                    2020L,       &quot;Jason Valdez&quot;,   &quot;Sciences&quot;,  &quot;164&quot;,         97,   99,
                    2020L,       &quot;Jason Valdez&quot;, &quot;Humanities&quot;,  &quot;231&quot;,         96,   98,
                    2019L,       &quot;Jason Valdez&quot;,   &quot;Sciences&quot;,  &quot;933&quot;,         96,   99,
                    2019L,       &quot;Jason Valdez&quot;, &quot;Humanities&quot;,  &quot;853&quot;,         95,   99,
                    2021L,       &quot;Eric Collins&quot;,   &quot;Sciences&quot;,   &quot;21&quot;,         92,   93,
                    2019L,       &quot;Eric Collins&quot;,   &quot;Sciences&quot;,  &quot;831&quot;,         94,   97,
                    2019L,       &quot;Eric Collins&quot;, &quot;Humanities&quot;,   &quot;10&quot;,         94,   97,
                    2021L, &quot;Sebastian Goldberg&quot;,   &quot;Sciences&quot;,   &quot;41&quot;,         93,   96,
                    2020L, &quot;Sebastian Goldberg&quot;,   &quot;Sciences&quot;,  &quot;476&quot;,         94,   98,
                    2020L, &quot;Sebastian Goldberg&quot;, &quot;Humanities&quot;,   &quot;81&quot;,         93,   96,
                    2019L, &quot;Sebastian Goldberg&quot;,   &quot;Sciences&quot;, &quot;1418&quot;,         95,   98
                    )
  
  output_df &lt;- tibble::tribble(
    ~year,           ~full_name,        ~type, ~count, ~avg_score, ~max,
    2021L,       &quot;Jason Valdez&quot;, &quot;Humanities&quot;,    59L,        95L,  96L,
    2020L,       &quot;Jason Valdez&quot;, &quot;Humanities&quot;,   231L,        96L,  98L,
    2021L,       &quot;Eric Collins&quot;,   &quot;Sciences&quot;,    21L,        92L,  93L,
    2019L,       &quot;Eric Collins&quot;,   &quot;Sciences&quot;,   831L,        94L,  97L,
    2021L, &quot;Sebastian Goldberg&quot;,   &quot;Sciences&quot;,    41L,        93L,  96L,
    2020L, &quot;Sebastian Goldberg&quot;,   &quot;Sciences&quot;,   476L,        94L,  98L
    )
  </code></pre>