title: dplyr with do assignments duplicate first value
body: <p>I want to assign a variable to the <code>.</code> data frame from but <code>dplyr</code> seems to do some magic behind the scenes that I don't understand. What is the proper way of performing the following:</p>
  
  <pre><code>data_frame(
    id = c(1,1,2,2,3,3),
    var = c(1:6)
  ) %&gt;% 
    group_by(id) %&gt;% 
    do({
      .$var = rep(max(.$var), times = nrow(.))
      .
    })
  </code></pre>
  
  <p>In it's current format it gives:</p>
  
  <pre><code># A tibble: 6 x 2
  # Groups:   id [1]
       id   var
    &lt;dbl&gt; &lt;int&gt;
  1     1     2
  2     1     2
  3     1     2
  4     1     2
  5     1     2
  6     1     2
  </code></pre>
  
  <p>I can get around it assigning the <code>.</code> to a new variable but this seems like a hack:</p>
  
  <pre><code>data_frame(
    id = c(1,1,2,2,3,3),
    var = c(1:6)
  ) %&gt;% 
    group_by(id) %&gt;% 
    do({
      alt = as_data_frame(.)
      alt$var = rep(max(alt$var), times = nrow(alt))
      alt
    })
  </code></pre>
  
  <p>Gives the expected result:</p>
  
  <pre><code># A tibble: 6 x 2
  # Groups:   id [3]
       id   var
    &lt;dbl&gt; &lt;int&gt;
  1     1     2
  2     1     2
  3     2     4
  4     2     4
  5     3     6
  6     3     6
  </code></pre>
  
  <h1>Complex example</h1>
  
  <p>By popular demand - here's an example more like what I'm trying to do. In short it's a form of imputation where it looks if there is a missing value in a series where:</p>
  
  <ol>
  <li>if the value before and after are the same then it is assumed that the value the same as the surrounding values. </li>
  <li>This assumption is only valid if a second variable indicates that less than a certain time has passed between the two time points. </li>
  <li>It also checks so that the missing value isn't the first or the last in the particular set. </li>
  <li>There may also be multiple missing values and therefore it tries to loop through each value.</li>
  <li>Surrounding values cannot be missing</li>
  </ol>
  
  <p>The code is:</p>
  
  <pre><code>data_frame(
    id = rep(1:5, each=3),
    var = c(
      'a', NA, 'a', 
      'b', NA, 'a',
      'b', NA, 'b',
      'a', 'b', 'b',
      NA, 'b', 'b'
    ),
    time = c(
      0, 1, 10, 
      0, 1, 10,
      0, 1, 20,
      0, 1, 10,
      0, 1, 10)
  ) %&gt;% 
    group_by(id) %&gt;% 
    do({
      for (i in which(is.na(.$var))) {
        if (i &gt; 1 &amp;&amp;
            i &lt; nrow(.) &amp;&amp;
            !is.na(.$var[i-1]) &amp;&amp;
            !is.na(.$var[i+1]) &amp;&amp;
            .$var[i-1] == .$var[i+1] &amp;&amp;
            (.$time[i+1] - .$time[i-1]) &lt; 14) {
          .$var[i] = .$var[i+1]
        }
      }
      .
    })
  </code></pre>
  
  <p>The same hack seems to do the job:</p>
  
  <pre><code>  ...
    do({
      retData &lt;- as_data_frame(.)
      for (i in which(is.na(.$var))) {
        if (i &gt; 1 &amp;&amp;
            i &lt; nrow(.) &amp;&amp;
            !is.na(.$var[i-1]) &amp;&amp;
            !is.na(.$var[i+1]) &amp;&amp;
            .$var[i-1] == .$var[i+1] &amp;&amp;
            (.$time[i+1] - .$time[i-1]) &lt; 14) {
          retData$var[i] = .$var[i+1]
        }
      }
      retData
    })
  </code></pre>
  
  <p>With the proper replacements:</p>
  
  <pre><code># A tibble: 15 x 3
  # Groups:   id [5]
        id   var  time
     &lt;int&gt; &lt;chr&gt; &lt;dbl&gt;
   1     1     a     0
   2     1     a     1
   3     1     a    10
   4     2     b     0
   5     2  &lt;NA&gt;     1
   6     2     a    10
   7     3     b     0
   8     3  &lt;NA&gt;     1
   9     3     b    20
  10     4     a     0
  11     4     b     1
  12     4     b    10
  13     5  &lt;NA&gt;     0
  14     5     b     1
  15     5     b    10
  </code></pre>