简体   繁体   中英

data.table: Check if all rows are NA by key

I am trying to drop entities in my data.table if all entries on a particular column are NA . Consider the data below:

dfX = data.table(read.table(textConnection(
  "id, t, value
  1, 1, 10 
  1, 2,NA
  2, 1,NA
  2, 2,NA"
), header = TRUE, sep = ","))
is.na(dfX$value)

Here, I would like to remove all rows for id ==2 , but not for id==1 .

Try

dfX[, .SD[!all(is.na(value))], id]

Or

dfX[dfX[, .I[!all(is.na(value))], id]$V1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM