How can I take a data.table object, and subset it conditionally on two columns by two paired vectors in another table. For instance I have a table of flights:
library(data.table)
library(nycflights13)
flights <- data.table(flights)
And I have another data.table containing my paired columns:
subDT <- data.table(sub_orig = c("EWR", "LGA"), sub_dest = c("IAH", "ATL"))
subDT
I wish to find and subset in flights
those rows where the origin is "EWR"
and the destination is "IAH"
exactly (i.e. like using &
) and also in the same query return the rows where origin is "LGA"
and where the destination is "ATL"
.
flights[dest %in% subDT[, sub_dest] & origin %in% subDT[, sub_orig]]
The above contains the data I want i.e. those pairings specified above, however it also contains pairings of "EWR" & "ATL"
and "LGA" & "IAH"
which I do not want.
I realise I can make a fake helper column (e.g. paste(dest, origin)
) and select via %in%
by that but I feel like there is a better approach, possibly some magic with setkey
that currently escapes me?
N.B. my end use case will actually be using a combination of character vector as one column, and a numeric factor as the other, if that is important.