问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/31955128/r-for-each-row-in-a-data-frame-how-to-check-if-at-least-one-column-is-not-na