Set operations/Anti_join

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.


#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
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.

#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
1 5
2 6
3 7
Thanks so much,


Ambassador to the humans
I'm not a huge fan of the tidyverse. Any reason you don't want to keep all of those in a list for the second part though? You stuffed everything into a temporary list for the first section - seems like saving that and operating on the list would be the simplest approach.