I am trying to do something very simple with data.table
and I lost the idiomatic way to do it
library(data.table)
set.seed(1)
DT = data.table(a=sample(letters,1e5,T), b=sample(letters,1e5,T), c=rnorm(1e5))
DT2 = data.table(a=sample(letters,5,T), b=sample(letters,5,T))
DT2
a b
1: k h
2: e v
3: f n
4: m q
5: w v
I want to select the rows of DT
that match those of DT2
.
As such the number of rows after operation will always be smaller that the initial table.
I want something doing this:
> DT[paste(a,b) %chin% DT2[,paste(a,b)]]
a b c
1: m q -0.4974579
2: e v -0.1325602
3: w v -1.8081050
4: m q 0.9025120
5: w v -0.4958802
---
729: f n 0.5604650
730: f n -1.2607321
731: m q 0.5146013
732: m q -1.8329656
733: k h -0.9752011
> DT2[paste(a,b) %chin% DT[,paste(a,b)]]
a b
1: e v
2: f n
3: k h
4: m q
5: w v
>
An inner join should do:
Produces:
If you want to know which values in
DT2
exist inDT
then: