This is a pretty usefull function to compare two objects (e.g. vectors), to see which elements of those two are different. Setdiff = set difference.
Load library, and set.seed() to get the same results as here.
set.seed(123)
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] "h" "u" "k" "w" "y" "b" "n" "x" "o" "l"
y
## [1] "y" "l" "r" "o" "c" "x" "g" "b" "i" "y"
Time to compare:
setdiff(x, y)
## [1] "h" "u" "k" "w" "n"
You have just got the elements of x that are different from y
Thus, the order of the compared objects does matter. If you want to establish which elements of y are different from x change the order!
setdiff(y, x)
## [1] "r" "c" "g" "i"
Cool, right?