Reputation: 2977
I have two character vectors with the different set of names and values:
x <- c("a", "b", "c", "d", "e")
names(x) <- c("foo", "bar", "baz", "qux", "grault")
y <- c("c", "a", "d", "b")
names(y) <- c("bar", "foo", "qux", "corge")
Is there a way to compare x
and y
so that we know their values corresponding to the name bar
are different because here x.bar = "b"
and y.bar = "c"
? Please note the names are not ordered. I tried setdiff
and which(x != y)
but neither one gives me the correct answer. Thanks!
Upvotes: 5
Views: 10675
Reputation: 44634
You could do this:
x[intersect(names(x), names(y))] == y[intersect(names(x), names(y))]
Upvotes: 8