Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching 2 almost same dataframes to be equal

Tags:

r

I have a df1 like this:

text 1
text 2
text 3
text 4
text 5

And another one df2 like this:

text 1
text 2
text 3
text 5

The problem is my dfs are almost the same, they have many rows and I can find the one that is additional to the first df in order to know who is it?

Is there any possible option to compare the two dfs and find the one row it is the difference between them?

like image 456
Ster32 Avatar asked Dec 06 '22 20:12

Ster32


1 Answers

You can rbind the two and then find the non-duplicated rows.

For example, if you have data frames a and b then

x <- rbind(a, b)
x[!duplicated(x) & !duplicated(x, fromLast = TRUE), ]
#     V1 V2
# 4 text  4

Or if you prefer, you can use dplyr::setdiff(), which has a data frame method.

dplyr::setdiff(a, b)
#     V1 V2
# 1 text  4

where

a <- read.table(text = "text 1
text 2
text 3
text 4
text 5", header = FALSE)

b <- a[-4, ]
like image 103
Rich Scriven Avatar answered Dec 26 '22 18:12

Rich Scriven