Search code examples
rmergedata-cleaning

R - Merge data, save failed output


I've merged some data, and it works great. But I'd like to know what data that's left out, i.e. data that didn't "find" matching data and was thus discarded. Is there a way to do something like this:

not_merged<-!merge(df1,df2)

The code above is pseudo code. The code below isn't.

df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
merged<-merge(df1,df2)

Solution

  • You can use an anti_join from the dplyr package:

    library(dplyr)
    
    anti_join(df1, df2)