Sometimes, I have a keyed data.table
which I'd like to subset according to its key and an unkeyed column. What's the simplest/fastest way to do this?
What feels most natural is an error:
dt <- data.table(id = 1:100, var = rnorm(100), key = "id")
dt[.(seq(1, 100, 2)) & var > 0, ]
The next cleanest thing is to chain:
dt[.(seq(1, 100, 2))][var > 0, ]
And of course we can ditch using binary search at all (I think this is clearly to be avoided):
dt[id %in% seq(1, 100, 2) & var > 0, ]
Is there an approach I'm missing? Also, any particular reason why the first is an error? The syntax seems clear enough to me.