Function for finding non-identical elements in two vectors

A simple question, but did not find it in stackoverflow. Is there a function to find all non-identical values:

x <- c("a","b","c","d") y <- c("a","f","g","c","d") 

the result should be:

 res <- c("b","f","g") 

All functions work only for one vector. setdiff() etc.

+4
source share
3 answers

This appeared on the Tony Breyal blog a few years ago, you can see several solutions there, here is the shortest:

 c(setdiff(x,y),setdiff(y,x)) 
+6
source
 setdiff(union(x, y), intersect(x, y)) 
+4
source

Longhand Form:

 c(x[!x %in% y],y[!y %in% x]) #[1] "b" "f" "g" 
+1
source

Source: https://habr.com/ru/post/1497181/


All Articles