Dean MacGregor
Dean MacGregor

Reputation: 18331

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

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.

Upvotes: 2

Views: 7052

Answers (3)

Dean MacGregor
Dean MacGregor

Reputation: 18331

From the comments of @docendodiscimus

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

Upvotes: 2

wolfste4
wolfste4

Reputation: 39

You could do this:

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

Upvotes: -1

Spacedman
Spacedman

Reputation: 94162

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

Upvotes: 11

Related Questions