Translate

Friday, 10 April 2015

Intersection & Union


Let's set vectors
Group1<-c("A","R","t","Y","p","Q") 
Group2<-c("Z","R","t","O","p","X")

> Group1  
[1] "A" "R" "t" "Y" "p" "Q"


> Group2   
[1] "Z" "R" "t" "O" "p" "X"


Intersection: there are 2 ways
> intersect(Group1,Group2)
[1] "R" "t" "p"

> Group1[which(Group1%in%Group2)]
[1] "R" "t" "p"


























Union:
> union(Group1,Group2)   
[1] "A" "R" "t" "Y" "p" "Q" "Z" "O" "X"

























Elements exclusive to Group 1: there are 2 ways
> setdiff(Group1,Group2)  
[1] "A" "Y" "Q"   

> Group1[which(!Group1%in%Group2)]   
[1] "A" "Y" "Q"
























Elements exclusive to Group 2: there are 2 ways
> setdiff(Group2,Group1)   
[1] "Z" "O" "X"

> Group2[which(!Group2%in%Group1)]  
 
[1] "Z" "O" "X"

























Mutually exclusive elements
> union(setdiff(Group1,Group2),setdiff(Group2,Group1))   
[1] "A" "Y" "Q" "Z" "O" "X"














No comments:

Post a Comment