'Remove rows of a data frame from another dataframe but keep duplicated in R

I'm working in R and I have two dataframes, one is the base dataframe, and another has the rows that i need to remove from the base one. But I can't use setdiff() function, because it removes duplicated rows. Here's an example:

a <- data.frame(var1 = c(1, NA, 2, 2, 3, 4, 5),
                var2 = c(1, 7, 2, 2, 3, 4, 5))

b <- data.frame(id = c(2, 4),
                numero = c(2, 4))

And the result must be:

id numero
1 1
NA 7
2 2
3 3
5 5

It must be an efficient algorithm, too, because the base dataframe has 3 million rows with 26 columns.



Solution 1:[1]

We may need to create a sequence column before joining

library(data.table)
setDT(a)[, rn := rowid(var1, var2)][!setDT(b)[, 
    rn:= rowid(id, numero)], on = .(var1 = id, var2 = numero, rn)][, 
     rn := NULL][]

-output

   var1  var2
   <num> <num>
1:     1     1
2:    NA     7
3:     2     2
4:     3     3
5:     5     5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun