All the matching values are found with the intersect
function, from the Set Operations. All the values in both variables are found with the union
function. So the values that are in the union, but not in the intersect are non-matching.
var1 <- LETTERS[1:5]
var2 <- LETTERS[4:8]
matched <- intersect(var1, var2)
all <- union(var1, var2)
non.matched <- all[!all %in% matched]