Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing unique elements over multiple vectors in R

Tags:

r

unique

vector

I have three vectors (a, b, and c) and found the common elements in all three using

common<-Reduce(intersect,list(a,b,c))

I want to find out now which elements are unique to a. I can't use a[!(a%in%common)] since that could return elements in a and one other vector.

I don't think this is a new or unique question, but part of my problem in finding the answer is that I'm not sure what the a[!(a%in%common)] function is called.

like image 329
KES Avatar asked Jun 25 '13 04:06

KES


2 Answers

You can use reduce and setdiff for an arbitrary length list

Reduce(setdiff, list(a,b,c))
like image 65
user1609452 Avatar answered Sep 21 '22 12:09

user1609452


The simple solution would be a[!(a %in% union(b,c))].

like image 37
Ricardo Oliveros-Ramos Avatar answered Sep 22 '22 12:09

Ricardo Oliveros-Ramos