Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary search based subset on NA values in data.table

Tags:

r

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!

like image 712
user3242624 Avatar asked Jan 28 '14 00:01

user3242624


Video Answer


1 Answers

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.

like image 98
Jacob Avatar answered Oct 05 '22 03:10

Jacob