R - For each row in a data frame, how to check if at least one column is not NA? [duplicate]

人盡茶涼 提交于 2019-12-20 02:27:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!