10

The Problem

I have two string vectors of different lengths. Each vector has a different set of strings. I want to find the strings that are in one vector but not in both; that is, the symmetric difference.

Analysis

I looked at the function setdiff, but its output depends on the order in which the vectors are considered. I found the custom function outersect, but this function requires the two vectors to be of the same length.

Any suggestions?

Correction

This issue seems to be specific to the data with which I am working. Otherwise, the answer below addresses the problem I mention in this post. I will look to see what is unique about my data and post back if I learn anything that might be helpful to other users.

Gyan Veda
  • 6,309
  • 11
  • 41
  • 66

4 Answers4

23

Why not:

sym_diff <- function(a,b) setdiff(union(a,b), intersect(a,b))
Blue Magister
  • 13,044
  • 5
  • 38
  • 56
  • Thanks for the suggestion, but this function doesn't work; the output is incorrect. I think it gets tripped up by the fact that the vectors differ in length. – Gyan Veda Nov 05 '13 at 20:17
  • 3
    Can you post some example code in your question showing some sample inputs and what you expect to be the output? – Blue Magister Nov 05 '13 at 20:24
  • 6
    @user2932774, this seems to correctly answer the question you posted and it does not depend on the vectors being the same length although without sample data and expected output you may have miscommunicated your intent. – G. Grothendieck Nov 05 '13 at 20:25
  • I see what you're saying, when I use sample data sym_diff works. For some reason, it doesn't work on the data on which I originally wanted to apply this solution. Thanks again for the suggestion. – Gyan Veda Nov 05 '13 at 20:39
  • @user2932774 Right... so can you post the data on which the solution is not working? – Blue Magister Nov 05 '13 at 20:40
  • I'm new to StackOverflow and I don't know how to do that yet; I'll have to look into it. I'll probably not do so because I've already gotten a lot of down votes and I'm not sure what I'm doing wrong that is upsetting so many people. – Gyan Veda Nov 05 '13 at 20:43
  • 2
    @user2932774 In the `r` tag the community appreciates a well-researched question, and a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) where there is data. Otherwise it seems to be a well formed question. – Blue Magister Nov 05 '13 at 20:57
10

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.

sebpardo
  • 707
  • 11
  • 17
  • 1
    This doesn't need `unique` does it? Shouldn't the oputput of `setdiff(a,b)` be distinct from the output of `setdiff(b,a)` already? – Dannid Feb 02 '21 at 18:41
2

Here is another symmetric difference function, this one from the definition (that can be seen, for instance, in the Wikipedia page linked to in the question).

sym_diff3 <- function(a, b) union(setdiff(a, b), setdiff(b, a))

Including the function in the test run in this other answer by user sebpardo gives approximately the same timings, a little slower. Output omitted.

identical(sym_diff(cars1, cars2), sym_diff3(cars1, cars2))
#[1] TRUE

microbenchmark(sym_diff(cars1, cars2),
               sym_diff2(cars1, cars2), 
               sym_diff3(cars1, cars2),
               times = 10000L)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
1

You can use symdiff in dplyr since 1.1.0:

library(dplyr)
symdiff(1:3, 3:5)
#[1] 1 2 4 5

Note that this function is so far only available in the development version (as of 2022-10-23).

Maël
  • 45,206
  • 3
  • 29
  • 67