1

I have a dataframe with duplicate rows. I want to merge with another dataframe such that each duplicate row is accounted for.

For Example,

df1:

   A    B    C
1  100  200  300
2  400  500  600
3  100  200  300

df2:

   A    B    C
1  100  200  300
2  600  700  800

After Merge: It should return

   A    B    C
1  100  200  300

But when I did:

merge(x=df1, y=df2)

I get

   A    B    C
1  100  200  300
2  100  200  300

Its returning two rows despite df2 having that row only once

2
  • Does this answer your question? Remove duplicated rows Commented Oct 1, 2021 at 18:59
  • unique(merge(df1, df2)) Commented Oct 2, 2021 at 12:53

3 Answers 3

1

A contemporary tidyverse option.

library(dplyr)

df1 %>%
  bind_rows(df2) %>%
  filter(duplicated(.)) %>%
  distinct(across(everything()))
  
#     A   B   C
# 1 100 200 300

Data

df1 <- structure(list(A = c(100L, 400L, 100L), B = c(200L, 500L, 200L
), C = c(300L, 600L, 300L)), class = "data.frame", row.names = c(NA, 
                                                                 -3L))

df2 <- structure(list(A = c(100L, 600L), B = c(200L, 700L), C = c(300L, 
                                                                  800L)), class = "data.frame", row.names = c(NA, -2L))
Sign up to request clarification or add additional context in comments.

Comments

0

An option is match after pasteing the columns together

 df1[match(do.call(paste, df2), do.call(paste, df1), nomatch = 0),]
    A   B   C
1 100 200 300

data

df1 <- structure(list(A = c(100L, 400L, 100L), B = c(200L, 500L, 200L
), C = c(300L, 600L, 300L)), class = "data.frame", row.names = c(NA, 
-3L))

df2 <- structure(list(A = c(100L, 600L), B = c(200L, 700L), C = c(300L, 
800L)), class = "data.frame", row.names = c(NA, -2L))

2 Comments

Thank you. But I think I would run into problem is there are two rows in df2 too. or If df1 has 3 duplicate rows, and df2 has 2 matching rows.
@MeghanathL try the updated solution
0

Maybe we can use intersect in dplyr

> intersect(df1,df2)
    A   B   C
1 100 200 300

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.