intersect() in dplyr

This is a pretty usefull function to compare two objects (e.g. vectors), to see which elements of those two are the same. intersect = inter-section.

Load library, and set.seed() to get the same results as here.

set.seed(345)
library(dplyr) 

Create two vectors to be compared - use sample() to do it in an elegant way:

x <- sample(LETTERS, 10, replace = TRUE) 
y <- sample(LETTERS, 10, replace = TRUE) 

Quick look at vectors:

x 
##  [1] "F" "H" "K" "R" "L" "U" "K" "V" "M" "C"
y
##  [1] "G" "Y" "E" "Q" "Y" "C" "Y" "G" "Z" "W"

Time to compare:

intersect(x, y)  
## [1] "C"

Awesome, right?