title: Remove rows in R matrix where all data is NA
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6437164/removing-empty-rows-of-a-data-file-in-r">Removing empty rows of a data file in R</a>  </p>
  </blockquote>
  
  
  
  <p>How would I remove rows from a matrix or data frame where <em>all</em> elements in the row are NA?</p>
  
  <p>So to get from this:</p>
  
  <pre><code>     [,1] [,2] [,3]
  [1,]    1    6   11
  [2,]   NA   NA   NA
  [3,]    3    8   13
  [4,]    4   NA   NA
  [5,]    5   10   NA
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>     [,1] [,2] [,3]
  [1,]    1    6   11
  [2,]    3    8   13
  [3,]    4   NA   NA
  [4,]    5   10   NA
  </code></pre>
  
  <p>Because the problem with na.omit is that it removes rows with <em>any</em> NAs and so would give me this:</p>
  
  <pre><code>     [,1] [,2] [,3]
  [1,]    1    6   11
  [2,]    3    8   13
  </code></pre>
  
  <p>The best I have been able to do so far is use the apply() function:</p>
  
  <pre><code>&gt; x[apply(x, 1, function(y) !all(is.na(y))),]
       [,1] [,2] [,3]
  [1,]    1    6   11
  [2,]    3    8   13
  [3,]    4   NA   NA
  [4,]    5   10   NA
  </code></pre>
  
  <p>but this seems quite convoluted (is there something simpler that I am missing?)....</p>
  
  <p>Thanks. </p>