title: How to get dplyr `pull()` to recognize grouping in R?
body: <pre><code>library(tidyverse)
  df &lt;- tibble(col1 = c("a", "a", "b", "b"),
               col2 = c(2, NA, 10, 8))
  #&gt; # A tibble: 4 x 2
  #&gt;   col1   col2
  #&gt;   &lt;chr&gt; &lt;dbl&gt;
  #&gt; 1 a         2
  #&gt; 2 a        NA
  #&gt; 3 b        10
  #&gt; 4 b         8
  </code></pre>
  
  <p>I've got the data frame above that I'd like to perform the following logic on:</p>
  
  <ul>
  <li>Group by <code>col1</code></li>
  <li>With this <code>col1</code> grouping determine the largest <code>col2</code> value</li>
  <li>Populate this largest <code>col2</code> value as the <code>col3</code> value, for said grouping</li>
  </ul>
  
  <p>What you'd end up with is the data frame below.</p>
  
  <pre><code>#&gt; # A tibble: 4 x 3
  #&gt;   col1   col2  col3
  #&gt;   &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
  #&gt; 1 a         2     2
  #&gt; 2 a        NA     2
  #&gt; 3 b        10    10
  #&gt; 4 b         8    10
  </code></pre>
  
  <p>My attempt at the code is below, and I understand it doesn't work because my <code>dplyr::pull()</code> isn't written (by me) in a way that it got the grouping logic I intend. How do I get <code>dplyr::pull()</code> to recognize the grouping I intend, or perhaps there's a better approach to solve my problem.</p>
  
  <pre><code>df %&gt;% 
    group_by(col1) %&gt;% 
    mutate(col3 = top_n(., 1, col2) %&gt;% pull(col2))
  #&gt; # A tibble: 4 x 3
  #&gt; # Groups:   col1 [2]
  #&gt;   col1   col2  col3
  #&gt;   &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
  #&gt; 1 a         2     2
  #&gt; 2 a        NA    10
  #&gt; 3 b        10     2
  #&gt; 4 b         8    10
  </code></pre>