To remove the NA rows simply set nomatch=0:
Here is an example (I removed the random sampling so 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)] #1 - 8 are available, 11 and 12 are not
dt[J(needed_letters),nomatch=0]
Addition from Matt
Also, if you prefer nomatch=0 to be the default, you can change the default :
options(datatable.nomatch=0)
dt[J(needed_letters)] # now, no NAs will be returned
You can check all arguments like this :
> 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)
The arguments whose default is via getOption can therefore have their default changed.