With data.table, I'd like to subset a table according to another one.
DT1 <- data.table( A = c(1:5,1:2), B = c(1,1,1,2,2,2,3), C = c(1:3))
DT2 <- data.table( A = c(1, 2, 3), B = c(1,3,1), D = c(4:6))
The output should contain only columns of the first table.
A B C
1: 1 1 1
2: 2 3 1
3: 3 1 3
With the merge function I get columns of both table
merge(DT1, DT2, by = c("A", "B"))
A B C D
1: 1 1 1 4
2: 2 3 1 5
3: 3 1 3 6
Since my datasets have much more columns, I'm looking for a way to get the result without the merge function, but with the data.table syntax [i, j, by]. However, with subsetting the first table it doesn't merge correctly.
DT1[A %in% DT2[,A] & B %in% DT2[, B]]
A B C
1: 1 1 1
2: 2 1 2 < wrong
3: 3 1 3
4: 2 3 1
DT1[A == DT2[,A] & B == DT2[, B]]
A B C
1: 1 1 1
2: 3 1 3
Warning messages:
1: In A == DT2[, A] :
longer object length is not a multiple of shorter object length
2: In B == DT2[, B] :
longer object length is not a multiple of shorter object length
Is there a way to get the correct rows like with merge, but using the data.table Syntax [ ]?