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})
A good way to do this ist the setdiff()
function, whicht takes the two dataframes as arguments.
newdata <- setdiff(df1, df2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With