Say I have two tables:
library(data.table)
set.seed(1)
tab1 <- data.table(
let = rep(letters[1:2], each = 3),
num = rep(1:3, 2),
val = rnorm(6),
In this case, it's equivalent to an anti join:
tab1[!tab2, on=c("let", "num")]
But setdiff()
would only the first row for every let,num
. This is marked for v1.9.8, FR #547.
One solution would be to do a merge and remove the rows where there are values from tab2
d<-as.data.frame(merge(tab1,tab2,all=T))
t<-is.na(d[,4])
d[t,][,-4]
let num val.x
3 a 3 -0.8356286
6 b 3 -0.8204684
Using data.table
:
merge(tab1,tab2,all=T)[is.na(val.y), 1:3]
let num val.x
1: a 3 -0.8356286
2: b 3 -0.8204684