问题
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.table
s
回答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