Bugzilla – Bug 14786
merge.data.frame fails when suffix addition creates duplicate column name
Last modified: 2014-02-16 11:42:52 UTC
A bug occurs in merge.data.frame under a situation where a suffix addition for columns with common names creates a situation where one of the two data.frames now has two columns with the same name. This was initially reported at:
# Create data.
# Works fine.
# X1 X2.x X3 X4 X2.y
# 1 1 10 12 NA NA
# 2 11 NA NA NA 11
# Change the names of the columns.
# Same data fails!
# Error in match.names(clabs, names(xi)) :
# names do not match previous names
My own short analysis:
The error occurs in the "merge.data.frame" method, on this line:
x <- rbind(x, ya)
The problem is that "x" and "ya" don't share the same column names. That problem occurs on this line, just two lines before the previous one:
ya <- cbind(ya, x[rep.int(NA_integer_, nyy), nm.x, drop = FALSE])
nm.x" is a set of names c("v2.x","v2.y","v2.x"). and x is a data.frame with two columns with the name 'v2.x'. Interestingly, when you select the columns from this data.frame, it appears to rename one of the columns!
 "v" "v2.x" "v2.y" "v2.x"
 "v2.x" "v2.y" "v2.x"
v2.x v2.y v2.x.1
1 10 12 10
I tried to solve this by using the position of the column, instead of the name, but the resulting name is still changed (but the values are now what you want)!
v v2.x v2.y v2.x.1
1 1 10 12 NA
My version of R:
system x86_64, mingw32
svn rev 57956
version.string R version 2.14.1 (2011-12-22)
You are asking it to create a data frame with duplicated column names: this *not* the same merge.
Altered to give an explicit error message.
(spam comment removed)