Home > front end >  Find all NAs in R data.table
Find all NAs in R data.table

Time:05-18

Is there an elegant way to display all rows with NAs in a R data.table? I would come up with the following:

nas <- dt[is.na(dt)]

but this leads to an error:

Error in `[.data.table`(dt, is.na(dt)) : 
  i is invalid type (matrix). Perhaps in future a 2 column matrix could return a list of elements of DT (in the spirit of A[B] in FAQ 2.14). Please report to data.table issue tracker if you'd like this, or add your comments to FR #657.

the following works, but could maybe improved:

nas <- rbindlist(lapply(names(dt), function(col){
  dt[is.na(get(col))]
}))

CodePudding user response:

Count NA's in each column of a df object with dplyr package

 df %>%
      summarise_all(funs(sum(is.na(.))))

CodePudding user response:

I found the answer in the related questions:

nas <- dt[!complete.cases(dt)]

CodePudding user response:

This displays all rows of a data.table that contain NAs:

dt[is.na(rowSums(dt))]
  • Related