3

I have a data frame like this

col_1 col_2 col_3 col_4
12344 53445 34335 AAA
12545 56565 12123 AAB
NA    54556 32323 ABB
NA    NA    NA    NA
43434 65654 NA    ABA

I want to get rows with at least non-NA value, or put another way, rows with all NAs (row 5 in this case) should be removed. Can you give me some advice?

Garp
  • 379
  • 4
  • 10

1 Answers1

4

if your data frame is named dta:

dta[rowSums(!is.na(dta)) > 0, ]

This works by checking if each item is.na, taking the opposite !, taking the rowSums, finding those that are > 0 and then using [ to subset them.

jeremycg
  • 24,657
  • 5
  • 63
  • 74