title: replace row values conditional on is.na in another column
body: <p>Simple logical replace problem; I have a dataframe like:</p>
  
  <pre><code>mydf &lt;- expand.grid(var1 = c('type1', 'type2'), var2 = c(7, 6, "NA"), var3 = 9)
  </code></pre>
  
  <p>I would like to replace the values in var3 with the values in var2 unless var2 is NA. So the resulting new var3 should be 7,7,6,6,NA,NA. In trying to get at this, I notice that   </p>
  
  <pre><code>mydf$var3[mydf$var2 == 7] &lt;- 5
  </code></pre>
  
  <p>correctly identifies rows 1 and 2 of mydf as needing replacement, and leaves the last four rows alone, so I get var3 = 5,5,9,9,9,9. However if I try </p>
  
  <pre><code>    mydf$var3[!is.na(mydf$var2)] &lt;- 5
  </code></pre>
  
  <p>I get var3 = 5,5,5,5,5,5. So why didn't it skip the last two rows, where var2 was NA? Next problem is that don't know how to get the replacement values to be var2 instead of a constant. When I try </p>
  
  <pre><code>mydf$var3[!is.na(mydf$var2)] &lt;- mydf$var2
  </code></pre>
  
  <p>I get var3 = 1,1,2,2,3,3. Which I do not understand at all. </p>