[英]How can I find the index of rows that contain a vector of values in any order in an r data.table?
我有一個向量x:
x = c(3, 5, 6)
和一個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)
我想以任何順序過濾掉不包含x中所有三個值的所有數據行。 在這種情況下,數據將被子集為僅包含第3和第5行。
謝謝。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.