Find complement of a data frame (anti - join) Find complement of a data frame (anti - join) r r

Find complement of a data frame (anti - join)


You could also do some type of anti join with data.tables binary join

library(data.table)setkey(setDT(df), heads)[!df1]#    heads# 1:  row1# 2:  row2# 3:  row4

EDIT: Starting data.table v1.9.6+ we can join data.tables without setting keys while using on

setDT(df)[!df1, on = "heads"]

EDIT2: Starting data.table v1.9.8+ fsetdiff was introduced which is basically a variation of the solution above, just over all the column names of the x data.table, e.g. x[!y, on = names(x)]. If all set to FALSE (the default behavior), then only unique rows in x will be returned. For the case of only one column in each data.table the following will be equivalent to the previous solutions

fsetdiff(df, df1, all = TRUE)


Try anti_join from dplyr

library(dplyr)anti_join(df, df1, by='heads')


Try the %in% command and reverse it with !

df[!df$heads %in% df1$heads,]