0

I have these two data frames a and b I want to remove what is in a from b

example a =
     X  Y
1    1  3
2    2  4
3    3  5

example b = 
   X   Y   Z
1  3   5   4  --- want to remove this
2  4   6   2
3  1   3   2  --- want to remove this
4  2   3   4
5  5   3   4
6  2   4   2  ---  want to remove this
7  4   3   4
8  2   4   6 ----  want remove this
9  6   9   6
10 2   0   3

So I'm only keeping the rows that dont have the combination of a the final result would be this:

   X   Y   Z
1  4   6   2
2  2   3   4
3  5   3   4
4  4   3   4
5  6   9   6
6  2   0   3

Thanks

1 Answers1

0

anti-join from the dplyr package can be very helpful.

library(tidyverse)

a <- tibble(X=c(1, 2, 3), Y=c(3, 4, 5))


b <- tibble(X=c(3, 4, 1, 2, 5, 2, 4, 2, 6, 2),
            Y=c(5, 6, 3, 3, 3, 4, 3, 4, 9, 0),
            Z=c(4, 2, 2, 4, 4, 2, 4, 6, 6, 3))

c <- b %>% anti_join(a, by=c("X", "Y"))
c

Gives

# A tibble: 6 x 3
      X     Y     Z
  <dbl> <dbl> <dbl>
1     4     6     2
2     2     3     4
3     5     3     4
4     4     3     4
5     6     9     6
6     2     0     3
Limey
  • 10,234
  • 2
  • 12
  • 32
  • I agree with @sindri_baldur. A reproducible example would have been helpful. But in this case the question was simple enough. See [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) for advide on creating a reprex. – Limey May 29 '20 at 09:36
  • Thansk thats nice. How would I do the opposite, say I wanted to keep only whats in a in b, so there would be 4 rows returned, if that makes sense. – user123231322 May 29 '20 at 10:34
  • That would be a `left_join`. Full details are [here](https://dplyr.tidyverse.org/reference/join.html). – Limey May 29 '20 at 10:44