title: Mutate top n rows without throwing away the other rows
body: <p>I have the following <code>data.frame</code> below. I would like to create a new column <code>w</code> (for weight). <code>w</code> should equal 1 / n for the industries that have the n highest returns for each given date and should equal 0 for the rest of the industries. I can <code>group_by(date)</code> and use <code>top_n(3, wt = return)</code> to filter the top industries and then <code>mutate(w = 1/n)</code>, but how can I <code>mutate</code> without throwing away the other industries where <code>w</code> = 0?</p>
  
  <pre><code>structure(list(date = structure(c(16556, 16556, 16556, 16556, 
  16556, 16556, 16556, 16556, 16556, 16556, 16587, 16587, 16587, 
  16587, 16587, 16587, 16587, 16587, 16587, 16587, 16617, 16617, 
  16617, 16617, 16617, 16617, 16617, 16617, 16617, 16617), class = "Date"), 
      industry = c("Hlth", "Txtls", "BusEq", "Fin", "ElcEq", "Food", 
      "Beer", "Books", "Cnstr", "Carry", "Clths", "Txtls", "Fin", 
      "Games", "Cnstr", "Meals", "Hlth", "Hshld", "Telcm", "Rtail", 
      "Smoke", "Games", "Clths", "Rtail", "Servs", "Meals", "Food", 
      "Hlth", "Beer", "Trans"), return = c(4.89, 4.37, 4.02, 2.99, 
      2.91, 2.03, 2, 1.95, 1.86, 1.75, 4.17, 4.09, 1.33, 1.26, 
      0.42, 0.29, 0.08, -0.11, -0.45, -0.48, 9.59, 6, 5.97, 5.78, 
      5.3, 4.15, 4.04, 3.67, 3.51, 3.27)), row.names = c(NA, -30L
  ), class = c("tbl_df", "tbl", "data.frame"))
  
  # A tibble: 30 x 3
     date       industry return
     &lt;date&gt;     &lt;chr&gt;     &lt;dbl&gt;
   1 2015-05-01 Hlth       4.89
   2 2015-05-01 Txtls      4.37
   3 2015-05-01 BusEq      4.02
   4 2015-05-01 Fin        2.99
   5 2015-05-01 ElcEq      2.91
   6 2015-05-01 Food       2.03
   7 2015-05-01 Beer       2   
   8 2015-05-01 Books      1.95
   9 2015-05-01 Cnstr      1.86
  10 2015-05-01 Carry      1.75
  # ... with 20 more rows
  </code></pre>
  
  <p><strong>EDIT</strong>: How would you handle ties? Suppose there is a tie for third place. The third place weight should be split between 3rd and 4th place (assuming only 2 are tied) with weights of (1/n)/2. The 1st and 2nd place weights stay at 1/n.</p>
  
  <p><strong>EDIT</strong>: Suppose <code>n</code> = 3. The top 3 <code>A2</code> values for each <code>A1</code> should get a weight <code>w</code> of 1/3 <em>if there are no ties</em>. If there is a tie for 3rd place (T3), then we have (1st, 2nd, T3, T3) and I would like weights to be 1/3, 1/3, 1/6, 1/6 to maintain a total weight of 1. This is only for 3rd place however. (1st, T2, T2) should have weights of 1/3, 1/3, 1/3. (T1, T1, T2, T2) should have weights of 1/3, 1/3, 1/6, 1/6, etc. </p>
  
  <pre><code>structure(list(A1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
      2L), .Label = c("A", "B"), class = "factor"), A2 = c(1, 3, 3, 
      4, 5, 6, 7, 8, 8)), row.names = c(NA, -9L), class = "data.frame")
  </code></pre>
  
  <p>The output for <code>df</code> should be:</p>
  
  <pre><code>&gt; df
    A1 A2  w
  1  A  1  0 
  2  A  3  0.1666
  3  A  3  0.1666 
  4  A  4  0.3333
  5  A  5  0.3333
  6  B  6  0
  7  B  7  0.3333
  8  B  8  0.3333
  9  B  8  0.3333
  </code></pre>