oercim oercim - 24 days ago 20
R Question

Find complement of a data frame (anti - join)

I have two data frames(df and df1). df1 is subset of df. I want to get a data frame which is complement of df1 in df. For example let,

data frame df:

heads
row1
row2
row3
row4
row5


data frame df1:

heads
row3
row5


Then the desired output df2 is:

heads
row1
row2
row4

Answer

Try anti_join from dplyr

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