Thirst for Knowledge
Thirst for Knowledge

Reputation: 1628

Capturing matching and non-matching values between two dataframes in R

I've done quite a bit of reading on this but I've not been able to get an answer that works yet.

I've been using the setdiff function in R to look at the number of matches between two dataframes. I know that I have 71 out of 200 observations matching and the remainder non-matching.

So far I've just done this to get the number of matching and non-matching values:

check = setdiff(dataset1$variable1, dataset2$variable1)

How do I return a list of the matching and non-matching values?

Thanks,

Ed

Upvotes: 5

Views: 8115

Answers (1)

Edwin
Edwin

Reputation: 3242

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]

Upvotes: 7

Related Questions