title: R: How to include lm residual back into the data.frame?
body: <p>I am trying to put the residuals from lm back into the original data.frame:</p>
  
  <pre><code>fit &lt;- lm(y ~ x, data = mydata, weight = ind)
  mydata$resid &lt;- fit$resid
  </code></pre>
  
  <p>The second line would normally work if the residual has the same length as the number of rows of mydata. However, in my case, some of the elements of ind is <code>NA</code>. Therefore the residual length is usually less than the number of rows. Also <code>fit$resid</code> is a vector of "numeric" so there is no label for me to merge it back with the mydata data.frame. Is there an elegant way to achieve this?</p>