To delete lines NA, simply set nomatch=0:
Here is an example (I deleted a random sample so that everyone can have the same results)
library(data.table)
dt = data.table(foo = 1:10, bar = letters[1:10])
setkey(dt, bar)
needed_letters = letters[c(1:8,11,12)]
dt[J(needed_letters),nomatch=0]
Matt Add-on
Alternatively, if you prefer the nomatch=0default, you can change the default:
options(datatable.nomatch=0)
dt[J(needed_letters)]
You can check all the arguments as follows:
> args(data.table:::`[.data.table`)
function (x, i, j, by, keyby,
with = TRUE,
nomatch = getOption("datatable.nomatch"),
mult = "all",
roll = FALSE,
rollends = if (roll=="nearest") c(TRUE,TRUE)
else if (roll>=0) c(FALSE, TRUE)
else c(TRUE,FALSE),
which = FALSE,
.SDcols,
verbose = getOption("datatable.verbose"),
allow.cartesian = getOption("datatable.allow.cartesian"),
drop = NULL)
, , getOption, .