Remove rows in R matrix where all data is NA

Viewed 106435

Possible Duplicate:
Removing empty rows of a data file in R

How would I remove rows from a matrix or data frame where all elements in the row are NA?

So to get from this:

     [,1] [,2] [,3]
[1,]    1    6   11
[2,]   NA   NA   NA
[3,]    3    8   13
[4,]    4   NA   NA
[5,]    5   10   NA

to this:

     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    3    8   13
[3,]    4   NA   NA
[4,]    5   10   NA

Because the problem with na.omit is that it removes rows with any NAs and so would give me this:

     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    3    8   13

The best I have been able to do so far is use the apply() function:

> 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

but this seems quite convoluted (is there something simpler that I am missing?)....

Thanks.

2 Answers
Related