-2

How can I remove rows which include NA values in a subset of columns?

For the following data.frame, I want to remove rows which have NA in both columns ID1 and ID2:

name  ID1   ID2
a     NA    NA
b     NA    2
c     3     NA

I want to receive this one:

name  ID1   ID2
b     NA    2
c     3     NA
Zaynab
  • 233
  • 3
  • 16
  • complete.cases doesn't work. I want to remove only rows which have NA values in a subset of columns simultaneously. – Zaynab Jun 11 '16 at 08:52
  • That has also been answered in the linked question, you just need to read the linked question and its answers a little more carefully. By the answer of @Pierre Lafortune, `delete.na(final, 1)` does what you want (run the `delete.na` code from his answer in the linked question). – Therkel Jun 11 '16 at 10:12
  • A changed `df[rowSums(is.na(df))<2,]` from the accepted answer in the link also works ("less than two NA's allowed"). Again, as they note in the linked question, you can restrict on which columns to look at. – Therkel Jun 11 '16 at 10:15

2 Answers2

0
mydf[!(is.na(mydf$ID1) & is.na(mydf$ID2)),]
I.Eskw
  • 1
  • 2
0
subset(data, !(is.na(ID1) & is.na(ID2)))
zx8754
  • 52,746
  • 12
  • 114
  • 209
Joseph
  • 23
  • 5
  • Thanks for providing code which might help solving the problem, but generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem. – LordWilmore May 09 '18 at 10:39