2

My question if very much like a previous question (Remove rows with NAs (missing values) in data.frame). I would like to filter my matrix in such a way that all the column with just NA's will be gone. I want to keept the column with consist of a mixture of NA's and numbers. So in the example below, I would like to get rid of the columns named mmul and cfam and keep the rest.

             gene hsap mmul mmus rnor cfam
1 ENSG00000208234    0   NA   NA   NA   NA
2 ENSG00000199674    0   NA    2    2   NA
3 ENSG00000221622    0   NA   NA   NA   NA
4 ENSG00000207604    0   NA   NA    1   NA
5 ENSG00000207431    0   NA   NA   NA   NA
6 ENSG00000221312    0   NA    2    3   NA

I want my new matrix to look like this:

             gene hsap  mmus rnor 
1 ENSG00000208234    0   NA   NA  
2 ENSG00000199674    0    2    2  
3 ENSG00000221622    0   NA   NA  
4 ENSG00000207604    0   NA    1  
5 ENSG00000207431    0   NA   NA  
6 ENSG00000221312    0    2    3  
Community
  • 1
  • 1
Ashoka
  • 119
  • 1
  • 6

1 Answers1

1

you are looking for all(is.na()):

DF = data.frame(Col1 = c(NA,NA),Col2 = c(1,1));
DFOut = DF[!apply(DF,2,function(x) all(is.na(x)))]
David Go
  • 740
  • 1
  • 6
  • 13