-2

i have a question relating to deleting rows in data.table How can i delete rows in data.table which only contain NA values? There are some solutions here but it only works when it contains only numeric value. But my data also contains characteristic value so i can not use something like sumRows. Thanks for your help

Thanh Quang
  • 193
  • 1
  • 11

2 Answers2

4

We can use Reduce with is.na

dt[dt[,!Reduce(`&`, lapply(.SD, is.na))]]

data

dt <- data.table(col1 = c(1, NA, 2, NA, 3), col2 = c(2, NA, 3, 4, 5))
akrun
  • 874,273
  • 37
  • 540
  • 662
1
dt[apply(dt,1,function(r){!all(is.na(r))}),]
Nicholas Hamilton
  • 10,044
  • 6
  • 57
  • 88