49

Possible Duplicate:
R - remove rows with NAs in data.frame

How can I quickly remove "rows" in a dataframe with a NA value in one of the columns?

So

     x1  x2
[1,]  1 100
[2,]  2  NA
[3,]  3 300
[4,] NA 400
[5,]  5 500

should result in:

     x1  x2
[1,]  1 100
[3,]  3 300
[5,]  5 500
Community
  • 1
  • 1
waanders
  • 8,327
  • 21
  • 66
  • 100
  • 2
    See the answers there for a solution to your problem. And be aware that you show us a matrix and not a dataframe. – Joris Meys May 26 '11 at 12:55
  • 2
    Please at least *attempt* to search for your answer here (adding `[r]` to search within R questions), via Rseek.org or otherwise. – Dirk Eddelbuettel May 26 '11 at 13:05
  • Isn't that question concerned with ALL values in the row being NA's? and possibly NA's in specific columns? As such this is a slightly different (and easier) question. As is usual with easy and hard applications of the same concept, one can infer the easy answer from the hard, but that may be hard :) – Stuart R. Jefferys Aug 12 '16 at 18:58

1 Answers1

92
dat <- data.frame(x1 = c(1,2,3, NA, 5), x2 = c(100, NA, 300, 400, 500))

na.omit(dat)
  x1  x2
1  1 100
3  3 300
5  5 500
Chase
  • 62,262
  • 16
  • 135
  • 160