How to filter rows out of data.table where any column is NA without specifying columns individually

放肆的年华 提交于 2021-02-11 05:16:35

问题


Given a data.table

DT<-data.table(a=c(1,2,NA,4,5), b=c(2,3,4,NA,5),c=c(1,2,3,4,5),d=c(2,3,4,5,6))

how can I do the equivalent of

DT[!is.na(a) & !is.na(b) & !is.na(c) & !is.na(d)]

in a general form without knowing any of the column names or typing out the !is.na() for each individual column.

I could also do

DT[apply(DT,1,function(x) !any(is.na(x)))] but I'm wondering if there's a better way still.


回答1:


I think you are looking for complete.cases:

> DT[complete.cases(DT),]
   a b c d
1: 1 2 1 2
2: 2 3 2 3
3: 5 5 5 6



回答2:


From the comments of @docendodiscimus

data.table has an na.omit method which is optimized for data.tables




回答3:


You could do this:

DT[!is.na(rowSums(DT)),]


来源:https://stackoverflow.com/questions/33766492/how-to-filter-rows-out-of-data-table-where-any-column-is-na-without-specifying-c

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