When I say unexpected, I mean unexpected for me. Allow me to illustrate. We have the two data.frames:
b1<-data.frame(a=c("a","b"),b=1:2)
b2<-data.frame(a=c("a","b"),c=1:2)
Merge produces the following
> merge(b1,b2)
a b c
1 a 1 1
2 b 2 2
But when we have the data.frames
b1<-data.frame(a=c("a","a"),b=1:2)
b2<-data.frame(a=c("a","a"),c=1:2)
merge produces the
> merge(b1,b2)
a b c
1 a 1 1
2 a 1 2
3 a 2 1
4 a 2 2
when I expect
a b c
a 1 1
a 2 2
Why the two different results?
This is by design. base
mergeusesmatchon columns specified (or not specified). In case 1, it found only a single match for each value ofaso there were no duplicates. But in case 2, it found two matches:for each a and therefore returned all possible matches. See
?mergefor more information.joininplyrhas the option of matching only the first match.