r - Combining vector and binary search in data.table -
sometimes, have keyed data.table
i'd subset according key and unkeyed column. what's simplest/fastest way this?
what feels natural error:
dt <- data.table(id = 1:100, var = rnorm(100), key = "id") dt[.(seq(1, 100, 2)) & var > 0, ]
the next cleanest thing chain:
dt[.(seq(1, 100, 2))][var > 0, ]
and of course can ditch using binary search @ (i think avoided):
dt[id %in% seq(1, 100, 2) & var > 0, ]
is there approach i'm missing? also, particular reason why first error? syntax seems clear enough me.
as of writing, native way do:
dt[.(seq(1, 100, 2)) & var > 0, j] #some expression j
is following:
dt[.(seq(1, 100, 2)), .sd[var > 0, j]]
the more work data.table
, more natural is, still looks bit unintuitive. c'est la vie.
Comments
Post a Comment