简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM