2

Suppose I have a dataframe

      Grp1 Grp2 Grp3
Trt1    NA    1   NA
Trt2     2    3   NA
Trt3     4   NA    5

I'd like to filter this down to only include rows where the number of non-NA values is greater than some total (in this case 2). So for this example I would like a result:

      Grp1 Grp2 Grp3
Trt2     2    3   NA
Trt3     4   NA    5
Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134

3 Answers3

6

You could use rowSums() and is.na() to filter the dataframe. This will coerce the values you are using to filter into a matrix (so it may have issues with very large dataframes), but it should do the trick.

df1[rowSums(!is.na(df1)) >= 2, ]
     Grp1 Grp2 Grp3
Trt2    2    3   NA
Trt3    4   NA    5

Data:

df1 <- read.table(header = T, text = "      Grp1 Grp2 Grp3
Trt1    NA    1   NA
Trt2     2    3   NA
Trt3     4   NA    5")
Andrew
  • 5,028
  • 2
  • 11
  • 21
2

You can do it this way :

count_na <- apply(data, 1, function(x) sum(is.na(x)))
data[count_na < 2,]

sample data:

  col1 col2 col3
1    1    1   NA
2   NA   NA    2
3   NA    3    3

new output:

  col1 col2 col3
1    1    1   NA
3   NA    3    3
Gainz
  • 1,721
  • 9
  • 24
1

Another option:

data[apply(data,1,function(x) sum(!is.na(x)) >= 2),]
Fino
  • 1,774
  • 11
  • 21