# Set operations/Anti_join

#### jamesmartinn

##### Member
Hi all,

I'm trying to accomplish a set of tasks that involve set operations/joins. I'm able to solve a bit of this using packages from the Tidyverse. I can't figure out if there's a nicer way to do the last task, please below.

The first task is self-explanatory. I have 3 vectors, and I'm trying to find among all 3, which elements are in common.

Code:
library(dplyr)
library(purrr)

#setup data
dfa <- data.frame(v=c(1,2,3,4,5))
dfb <- data.frame(v=c(1,2,3,4,6))
dfc <- data.frame(v=c(1,2,3,4,7))

#get all elements that are common in ALL 3 of the vectors
common = list(dfa, dfb, dfc) %>% purrr::reduce(inner_join)

common

> common
v
1 1
2 2
3 3
4 4
For the next task, I want to return a return a data frame where ONLY the unique elements are returned (i.e. non-overlapping elements), among the 3 vectors. Here is my solution, which works, though I'm wondering if there was another way using purrr.

Code:
#get uniques
uniquesa = dfa %>% anti_join(common)
uniquesb = dfb %>% anti_join(common)
uniquesc = dfc %>%  anti_join(common)

#put together
test <- rbind(uniquesa, uniquesb, uniquesc)
test

> test
v
1 5
2 6
3 7
Thanks so much,