Q:

How can I find the index of rows that contain a vector of values in any order in an r data.table?

I have a vector x:
x = c(3, 5, 6)

And a data.table:
data = data.table(a = 1:5, b = c(5, 9, 6, 2, 3), c = c(6, 4, 5, 3, 6), d = 5:10)

I would like to filter out all rows of the data that don't contain all three values in x in any order. In this case data would be subsetted to only include rows 3 and 5.
Thanks.

A:

library(gtools)

setkey(data, a, b, c)

data[as.data.table(permutations(3, 3, x)), nomatch = 0]
#   a b c d
#1: 3 6 5 7
#2: 5 3 6 9

