简体   繁体   中英

Binary search based subset on NA values in data.table

I am trying to remove rows from my data.table where either one of two columns contain NA value. I would like to utilize binary search using J function from data.table package. Here is what I tried:

DT = data.table(x=rep(c("a","b",NA),each=10), y=c(1,3,6))
setkey(DT,x)
DT
     x y
 1: NA 6
 2: NA 1
 3: NA 3
 4: NA 6
 5: NA 1
 6: NA 3
 7: NA 6
 8: NA 1
 9: NA 3
10: NA 6
11:  a 1
12:  a 3
13:  a 6
14:  a 1
15:  a 3
16:  a 6
17:  a 1
18:  a 3
19:  a 6
20:  a 1
21:  b 3
22:  b 6
23:  b 1
24:  b 3
25:  b 6
26:  b 1
27:  b 3
28:  b 6
29:  b 1
30:  b 3
     x y

To remove all rows where x is NA I tried:

DT[!J(NA_character_)]

...but it still returns the entire data table . Does anyone have an idea of what I am doing wrong? Thank you so much!

In the example as shown, I would do it like this:

DT <- DT[!is.na(x) & !is.na(y)]

Doesn't use the J() function as requested, but thought I'd share this anyways.

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