# Symmetric set differences in R

**Life in Code**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

My .Rprofile contains a collection of convenience functions and function abbreviations. These are either functions I use dozens of times a day and prefer not to type in full:

## my abbreviation of head() h

Or problems that I’d rather figure out once, and only once:

## example: ## between( 1:10, 5.5, 6.5 ) between = low & x low & x < high } }

One of these "problems" that's been rattling around in my head is the fact that setdiff(x, y) is asymmetric, and has no options to modify this.

With some regularity, I want to know if two sets are equal, and if not, what are the differing elements. setequal(x, y) gives me a boolean answer to the first question. It would *seem* that setdiff(x, y) would identify those elements. However, I find the following result rather counter-intuitive:

> setdiff(1:5, 1:6) integer(0)

I personally dislike having to type both setdiff(x,y) and setdiff(y,x) to identify the differing elements, as well as remember which is the reference set (here, the second argument, which I find personally counterintuitive). With this in mind, here's a snappy little function that returns the symmetric set difference:

symdiff symdiff(1:5, 1:6) == symdiff(1:6, 1:5) [1] TRUE

Tada! A new function for my .Rprofile!

**leave a comment**for the author, please follow the link and comment on their blog:

**Life in Code**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.