Function to find symmetric difference (opposite of intersection) in R?
Why not:
sym_diff <- function(a,b) setdiff(union(a,b), intersect(a,b))
Another option that is a bit faster is:
sym_diff2 <- function(a,b) unique(c(setdiff(a,b), setdiff(b,a)))
If we compare it with the answer by Blue Magister:
sym_diff <- function(a,b) setdiff(union(a,b), intersect(a,b))
library(microbenchmark)
library(MASS)
set.seed(1)
cars1 <- sample(Cars93$Make, 70)
cars2 <- sample(Cars93$Make, 70)
microbenchmark(sym_diff(cars1, cars2), sym_diff2(cars1, cars2), times = 10000L)
>Unit: microseconds
> expr min lq mean median uq max neval
>sym_diff(cars1, cars2) 114.719 119.7785 150.7510 125.0410 131.177 12382.02 10000
>sym_diff2(cars1, cars2) 94.369 100.0205 121.6051 103.8285 109.239 12013.69 10000
identical(sym_diff(cars1, cars2), sym_diff2(cars1, cars2))
>[1] TRUE
The speed difference between these two methods increases when the samples compared are larger (thousands or more), but I couldn't find an example dataset to use with that many variables.