Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function for finding non-identical elements in two vectors

Tags:

r

vector

Simple question, but didn't find it on stackoverflow. Is there a function for finding all non-identical values:

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

result should be:

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

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

like image 426
beginneR Avatar asked Dec 20 '22 01:12

beginneR


2 Answers

This popped up on Tony Breyal's blog a few years ago, you can see several solutions there, here's the shortest one:

c(setdiff(x,y),setdiff(y,x))
like image 174
Ben Avatar answered Jan 30 '23 22:01

Ben


setdiff(union(x, y), intersect(x, y))
like image 27
Hong Ooi Avatar answered Jan 31 '23 00:01

Hong Ooi