4

I am creating a data.table in R and setting a column to be used as key. When I try to retrieve values from the data table; for the rows where there is no match I get NA values back. I typically dont want that behavior in my search. Example below

library(data.table) 
dt <- data.table('foo'=seq(10),bar=sample(letters,10))
setkey(dt,bar)
dt[sample(letters,5)]


> dt[sample(letters,5)]
   b foo
1: x   4
2: q   2
3: u   8
4: s  NA
5: b  NA

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.