rset-difference

Comparing differences between two dataframes in R


I'm following the solution provided in Determining different rows between two data sets in R but the R code doesn't seem to work (getting no results) and I'm not sure why. Can someone help to explain what I have done wrong here?

df1 <- data.frame(id=c("632592651","633322173","634703802","634927873","635812953","636004739","636101211","636157799","636263106","636752420"),
        text=c("asdf","cat","dog","mouse","elephant","goose","rat","mice","kitty","kitten"))

df2 <- data.frame(id=c("632592651","633322173","634703802","634927873","635812953","636004739","636101211","636157799","636263106","636752420","636809222","2004722036","2004894388","2005045755","2005535472","2005630542","2005788781","2005809679","2005838317","2005866692"),
        text=c("asdf_xyz","cat","dog","mouse","elephant","goose","rat","mice","kitty","kitten","tiger_xyz","lion","leopard","ostrich","kangaroo","platypus","fish","reptile","mammals","amphibians_xyz"))

diff1 <- df1[setdiff(df1$refid, df2$refid),]

diff2 <- df2[!(intersect(df2$refid, df1$refid)),]

Solution

  • library(tidyverse)
    
    anti_join(df1,df2)
    
    Joining, by = c("id", "text")
             id text
    1 632592651 asdf
    
    
    
    anti_join(df2,df1)
    
    Joining, by = c("id", "text")
               id           text
    1   632592651       asdf_xyz
    2   636809222      tiger_xyz
    3  2004722036           lion
    4  2004894388        leopard
    5  2005045755        ostrich
    6  2005535472       kangaroo
    7  2005630542       platypus
    8  2005788781           fish
    9  2005809679        reptile
    10 2005838317        mammals
    11 2005866692 amphibians_xyz