如何在没有单独指定列的情况下从任何列为NA的data.table中过滤出行 [英] How to filter rows out of data.table where any column is NA without specifying columns individually
本文介绍了如何在没有单独指定列的情况下从任何列为NA的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))
我该怎么做
DT[!is.na(a) & !is.na(b) & !is.na(c) & !is.na(d)]
以常规形式,而无需知道任何列名或为每个单独的列键入!is.na()
.
in a general form without knowing any of the column names or typing out the !is.na()
for each individual column.
我也可以做
DT [apply(DT,1,function(x)!any(is.na(x)))]
但我想知道是否还有更好的方法.
DT[apply(DT,1,function(x) !any(is.na(x)))]
but I'm wondering if there's a better way still.
推荐答案
来自@docendodiscimus的评论
From the comments of @docendodiscimus
data.table
有一个 na.omit
方法,该方法针对 data.table
s
data.table
has an na.omit
method which is optimized for data.table
s
这篇关于如何在没有单独指定列的情况下从任何列为NA的data.table中过滤出行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文