I'm trying to find a way to filter DT with two keys using alternative instead of conjunction. Solution in dplyr would look like this:
filter(DF, A == a | B == b)
I'm trying to do the same thing in data.table
with key set on both A
and B
, but so far no luck.
I don't want to use DT[A == a | B == b]
form, because of lower performance of vector search.
Let's use the data below as an example:
DF <- data.frame(A = c(1, NA, 1, 2), B = c(NA, 3, 3, 5))
DF
# A B
# 1 1 NA
# 2 NA 3
# 3 1 3
# 4 2 5
filter(DF, A == 1 | B == 3)
# A B
# 1 1 NA
# 2 NA 3
# 3 1 3
DT <- as.data.table(DF)
setkey(DT, "A", "B")
Thanks to @Frank for an answer - it turned out to be the right way to do it. Frank proposed
mya = DT[A==a,which=TRUE]; myb = DT[B==b,which=TRUE]; DT[union(mya,myb)]
, as it does two binary searches.I did some benchmarks on larger dataset (97671 x 13) and this is how it looks like (some questionable attempts are also added; conjunction example added for comparison):