rdataframecompare-contrast

Create a new dataframe according to the contrast between two similar df


I have a dataframe made like this:

  X Y  Z T
  1 2  4 2
  3 2  1 4
  7 5 NA 3

After several steps (not important which one) i obtained this df:

  X Y Z T
  1 2 4 2
  3 2 NA 4
  7 5 NA 3

i want to obtain a new dataframe made by only the rows which didn't change during the steps; the result would be this one:

 X  Y  Z  T
 1  2  4  2
 7  5  NA 3

How could I do?


Solution

  • One option with base R would be to paste the rows of each dataset together and compare (==) to create a logical vector which we use for subsetting the new dataset

    dfO[do.call(paste, dfO) == do.call(paste, df),]
    #   X Y  Z T
    #1 1 2  4 2
    #3 7 5 NA 3
    

    where 'dfO' is the old dataset and 'df' is the new