Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Report the differences between two data frame in R

I have two data frames which are loaded from csv files. Basically from different environment but similar format/columns, They can have differences in rows/values. I would like to find the differences and create them in a new data frame. Also both data frame will have same order. I have 100's of files to be compared. Thanks in advance.

Dataframe1: df1test

product | country | partner | value
------------------------------------
prdct1  |  china  | part1   | ["563,45"]
prdct2  |  UK     | part4   | ["52,455"]
prdct3  |  USA    | part2   | ["563,45"]
prdct4  |  ITALY  | part6   | ["674,45"]
prdct5  |  UK     | part7   | ["563,578"]

Dataframe2: df1prod

product | country | partner | value
------------------------------------
prdct1  |  china  | part1   | ["563,45"]
prdct2  |  UK     | part4   | ["247,455"]
prdct3  |  USA    | part41  | ["563,45"]
prdct4  |  UK     | part6   | ["0,45"]

I would like to show the differences in third data frame

Dataframe3: dfDifference

Env:test                             Env:prod    
product| country|partner| value      product| country | partner | value
------------------------------------ -----------------------------------
prdct2 | UK     |part4 | ["52,455"]  prdct2 |UK |part4  | ["247,455"]
prdct3 | USA    |part2 | ["563,45"]  prdct3 |USA|part41 | ["563,45"]
prdct4 | ITALY  |part6 | ["674,45"]  prdct4 |UK |part6  | ["0,45"]
prdct5 | UK     |part7 | ["563,578"] Not Available

I tried the following functions and methods but did'nt workout

Compare function
    comptest<-compare(df1test,df1prod,allowAll = TRUE)

Variable combine
    df1test$Varcomp <- apply(df1test,1,paste,collapse=';')
    df1prod$Varcomp <- apply(df1prod,1,paste,collapse=';')
    aabb<-sapply(df1prod$Varcomp,FUN = function(x){x==df1test$Varcomp})
like image 620
fairplay Avatar asked Feb 04 '23 20:02

fairplay


1 Answers

A good way to do this ist the setdiff() function, whicht takes the two dataframes as arguments.

newdata <- setdiff(df1, df2)
like image 154
Marcel Der Avatar answered Feb 07 '23 10:02

Marcel Der