2017-06-10 86 views
0

我有兩個data.table dt和DT1它看起來像:篩選記錄在其他表基礎上的記錄

> dt <- data.table(grp = c("A", "A", "B", "B", "C"), 
        cat = c("01", "02", "01", "02", "01"), 
        Value = c(234, 234, 235, 536, 235)) 

> dt 
    grp cat Value 
1: A 01 234 
2: A 02 234 
3: B 01 235 
4: B 02 536 
5: C 01 235 

> dt1 <- data.table(grp = c("A","A","A","A","A","A","B","B","B", "B","C"), 
        cat = c("01","01","02","02","03","04", "01","01", "02", "03","01"), 
        rec = c(5435,4341, 32525,436,7087,467,523,245,568,24,789), 
        val = c(346,6876,436,6807,465,65875,6432,754,326532,746,578)) 

> dt1 
    grp cat rec val 
1: A 01 5435 346 
2: A 01 4341 6876 
3: A 02 32525 436 
4: A 02 436 6807 
5: A 03 7087 465 
6: A 04 467 65875 
7: B 01 523 6432 
8: B 01 245 754 
9: B 02 568 326532 
10: B 03 24 746 
11: C 01 789 578 

我想從表中刪除dt1記錄,我不具有相應的catgrp in dt

例如對於grp A,我沒有記錄與在012中的cat 03和04相關聯。所以我想在dt1中刪除它們。

我的決賽桌dt1必須看起來像

> dt1 
    grp cat rec val 
1: A 01 5435 346 
2: A 01 4341 6876 
3: A 02 32525 436 
4: A 02 436 6807 
5: B 01 523 6432 
6: B 01 245 754 
7: B 02 568 326532 
8: C 01 789 578 

我怎樣才能做到這一點與data.table R中

+2

'DT1〔DT,上=。 (grp,cat)]' – Jaap

+0

@Jaap根據他人更改評論回答不好 – akrun

+0

@akrun在我看到您的回答之前更改了評論 – Jaap

回答

0

我們可以做

dt1[dt[, -3], on = .(grp, cat)] 
# grp cat rec val 
#1: A 01 5435 346 
#2: A 01 4341 6876 
#3: A 02 32525 436 
#4: A 02 436 6807 
#5: B 01 523 6432 
#6: B 01 245 754 
#7: B 02 568 326532 
#8: C 01 789 578 
+0

@Jaap如果我找到dup e鏈接或相關鏈接,我會做。 – akrun

+0

如果你發現重複,你應該關閉它imo – Jaap

+5

@Jaap我不明白,如果你關閉而不是回答,你怎麼會得到repz? – Henrik