title: Average across Columns in R, excluding NAs
body: <p>I can't imagine I'm the first person with this question, but I haven't found a solution yet (here or elsewhere). </p>
  
  <p>I have a few columns, which I want to average in R. The only minimally tricky aspect is that some columns contain NAs. </p>
  
  <p>For example:</p>
  
  <pre><code>Trait Col1 Col2 Col3
  DF    23   NA   23
  DG    2    2    2
  DH    NA   9    9
  </code></pre>
  
  <p>I want to create a Col4 that averages the entries in the first 3 columns, ignoring the NAs. 
  So: </p>
  
  <pre><code> Trait Col1 Col2 Col3 Col4
   DF    23   NA   23   23
   DG    2    2    2    2
   DH    NA   9    9    9 
  </code></pre>
  
  <p>Ideally something like this would work:</p>
  
  <pre><code>data$Col4 &lt;- mean(data$Chr1, data$Chr2, data$Chr3, na.rm=TRUE)
  </code></pre>
  
  <p>but it doesn't. </p>